json、JSONObject、JSONArray的应用

type.java

package jiekou.duixiang;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import util.Constant;

public class Type {

private String id;
private String name;
private String image;
private String level;
private Date time;

public Type() {
super();
}
public Type(Object[] obj) {
super();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
this.id = String.valueOf(obj[0]);
this.name = String.valueOf(obj[1]);
this.image = Constant.IMG_PATH+String.valueOf(obj[2]);
this.level=String.valueOf(obj[3]);
try {
this.time = format.parse(String.valueOf(obj[4]));
} catch (ParseException e) {
this.time = null;
e.printStackTrace();
}
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("{");
sb.append(id).append(",").append(name).append(",").append(image).append(",").append(level).append(",").append(time);
sb.append("}");
return sb.toString();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}

}

ResponseResult.java

package jiekou.tool;

public class ResponseResult<T> {

/**
* 消息类型
*/
public String messageType;
/**
* 消息对象
*/
public T info;
public String getMessageType() {
return messageType;
}
public void setMessageType(String messageType) {
this.messageType = messageType;
}
public T getInfo() {
return info;
}
public void setInfo(T info) {
this.info = info;
}
public ResponseResult(String messageType, T info) {
super();
this.messageType = messageType;
this.info = info;
}
public ResponseResult(T info) {
super();
this.info = info;
}
public ResponseResult(String messageType) {
super();
this.messageType = messageType;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("{");
sb.append(messageType).append(",").append(info);
sb.append("}");
return sb.toString();
}
}

Test.java

package test;

import java.util.ArrayList;
import java.util.List;

import jiekou.duixiang.Type;
import jiekou.duixiang.YongHu;
import jiekou.tool.ResponseResult;
import net.sf.json.JSONArray;

import org.apache.commons.codec.digest.DigestUtils;

import util.Constant;
import dao.impl.CommentDaoImpl;
import dao.impl.InstructionDaoImpl;
import dao.impl.TreeNodeDaoImpl;
import dao.impl.UserDaoImpl;
import entity.User;

public class test {
public static String getMd5Password(String userPwd, String salt) {
userPwd=DigestUtils.sha1Hex(userPwd);
userPwd=DigestUtils.md5Hex(userPwd+salt);
return userPwd;
}

public static void main(String[] args) {
/******************对象************************/
UserDaoImpl daoImpl=new UserDaoImpl();
InstructionDaoImpl instructionDaoImpl=new InstructionDaoImpl();
CommentDaoImpl commentDaoImpl=new CommentDaoImpl();
YongHu yh=new YongHu();
String userStr="admin";
String password="123123";
String token="1";
User user=daoImpl.getfindUserAccount(userStr);
if(user!=null){
password=getMd5Password(password,String.valueOf(user.getSalt()));
if(!user.getUserPwd().equals(password)){
}else{
int upload=instructionDaoImpl.getUpload(user.getUserAccount());
int collection=0;
int comment=commentDaoImpl.getComment(user.getUserAccount());
yh=new YongHu(token, user.getUserAccount(), Constant.IMG_PATH+user.getUserImage(), user.getUserName(),
user.getEmail(), user.getPhone(), user.getSex(), user.getProvince(),
user.getCity(), user.getRegisterTime(), user.getQq(), user.getAddress(), user.getBirthday(), user.getRemark(),
user.getSign(), collection, upload, comment);
JSONArray jsonArray =null;
ResponseResult<YongHu> a=new ResponseResult<YongHu>("0",yh);
List<String> types=new ArrayList<String>();
types.add(a.toString());
jsonArray=JSONArray.fromObject(types);
System.out.println(jsonArray.toString());
}
}else{
}
/******************数组************************/
JSONArray jsonArray =null;
TreeNodeDaoImpl daoImpl2=new TreeNodeDaoImpl();
String update_time="2015-04-03";
List<?> list=daoImpl2.getTime(update_time);
Type type=new Type();
List<String> types=new ArrayList<String>();
for(int i=0;i<list.size();i++) {
Object[] obj = (Object[]) list.get(i);
type=new Type(obj);
types.add(type.toString());
}
jsonArray=JSONArray.fromObject(types);
System.out.println(jsonArray.toString());

}
}

说明:

把JSONArray[{"id":"1","name":"啦啦","age":"18"},{"id":"2","name":"慢慢","age":"19"}] 转成 [{1,啦啦,18},{1,慢慢,19}]把key去掉,只显示value

时间: 2024-10-09 00:59:49

json、JSONObject、JSONArray的应用的相关文章

JSON JSONObject.fromObject JSONArray.fromObject

public static void test1(){ Person person = new Person(); person.setName("zs"); person.setAge(20); JSONObject json = JSONObject.fromObject(person); String str = json.toString(); System.out.println(str); } public static void test2(){ Person perso

android Json 解析 JSONObject JSONArray

android Json 解析  JSONObject JSONArray 我只解析了一个,不管有多少个都以此方法类推,加上http://blog.csdn.net/menglele1314/article/details/46324069里的JSONArray解析,可以应付一般所以的开发了 try { String value = "{\"result\":\"S\", \"address\":[{\"shengfen\&q

net.sf.json JSONObject与JSONArray使用实例

实例自己想的一个实例应用场景:一个人可以有多个角色,例如:在家中是儿子,在学校是学生,在公司是程序员,一个人还可以办好多业务 * 每个业务好多个人都可以办,则标记(mark)就是记录这唯一标识的(如id)业务和称职 1.人实体类(People) package com.hsinfo.web.Demo; import java.util.Set; /** * @Description:人的实体类 * @date 2018年7月22日,下午8:58:03 */ public class People

net.sf.json.JSONObject 和org.json.JSONObject 的差别(转)

net.sf.json.JSONObject 和org.json.JSONObject  的差别. 一.创建json对象 String str = "{\"code\":\"0000\", \"msg\":{\"availableBalance\":31503079.02}} org.json.JSONObject: JSONObject json = new JSONObject(str); net.sf.json

net.sf.json.JSONObject 和org.json.JSONObject 的差别

net.sf.json.JSONObject 和org.json.JSONObject  的差别. 一.创建json对象 String str = "{\"code\":\"0000\", \"msg\":{\"availableBalance\":31503079.02}} org.json.JSONObject: JSONObject json = new JSONObject(str); net.sf.json

org.bson.codecs.configuration.CodecConfigurationException: Can&#39;t find a codec for class org.json.JSONObject$Null.

以及:java.lang.ClassCastException: org.json.JSONObject$Null cannot be cast to java.lang.Long 参考 :http://blog.csdn.net/u010823097/article/details/51780932 问题: 使用 Java MongoDB Driver < version: 3.2.2 > 的过程中,Updates 方法(MongoDB API Docs)会出现一个棘手的问题.比如 set(

jsonobject 遍历 org.json.JSONObject

[html] view plain copy import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; [java] view plain copy public static void main(String[] args) { String str = "{'TI':[{'value':'aa1','count':10},{'value':'aa2','count':15},{'

org.json.JSONObject and no properties discovered 错误解决

自己在搭建SSM框架的时候(Spring + spring mvc + mybatis)报错内容如下: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) 报错的意思是用的这个JSONObject对象由

String JSONObject JSONArray 转换

JSON使用阿里的fastJson为依赖包 gradle依赖管理如下: compile group: 'com.alibaba', name: 'fastjson', version:'1.2.41' 1.String转JSONObject 前言:String 是JSONObject格式的字符串 eg: JSONObject jSONObject = JSONObject.parseObject(String); 2.String转JSONArray 前言:String 是JSONArray格式

java.lang.ClassNotFoundException: net.sf.json.JSONObject

先去检查依赖包的问题 使用net.sf.json需要导入的包 如果jar 文件都存在. 右键项目选择Build path 在develoyment Assembly 添加 maven dependencies 网友回答1: 我已经导入了json的jar包及其需要的依赖包,过程是:右键项目选择Build path –> config build path –>add extenal jar加入以上所需要的jar.此时发现在servlet中写的关于JSONObject的代码不发生错误了,然后运行程