java操作json
JSON-lib是一个开源的java类库
1.可以把beans,maps, collections, java arrays and XML 转换成JSON对象
2.可以把JSON对象转换成beans
使用java操作json有一点要注意,那就是在进行对象转json的时候。如果对象中有未被这些jar包所实现转换的属性,比如说date类型
那么需要实现JsonValueProcessor()这个方法自行定义
比如:
import java.sql.Date;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
public class DateValueProcessor implements JsonValueProcessor{
@Override
public Object processArrayValue(Object arg0, JsonConfig arg1) {
// TODO Auto-generated method stub
return this.processor(arg0);
}
@Override
public Object processObjectValue(String arg0, Object arg1, JsonConfig arg2) {
// TODO Auto-generated method stub
return this.processor(arg1);
}
/*
* 自定义日期时间处理方法
*/
private Object processor(Object obj) {
Date date=(Date)obj;
return date==null?"":date.toString();
}
}
常见json操作都在注释里边了
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
public class Demo1 {
public static void main(String[] args) {
// 定义json对象
// JSONObject jo=new JSONObject();
// jo.element("name", "jack");
// jo.element("age", 23);
// jo.element("hasCar", false);
//
// System.out.println(jo);
// System.out.println(jo.get("age"));
//定义json数组
// JSONArray ja=new JSONArray();
// ja.add("jack");
// ja.add(true);
// ja.add(34);
//
// System.out.println(ja);
//from java to json
// String str="{'name':'Ada','age':23,'married':true}";
// JSONObject jo=(JSONObject)JSONSerializer.toJSON(str);
//
// System.out.println(jo.get("name"));
// Map<String, Object> map=new HashMap<>();
// map.put("name", "jack");
// map.put("age", 23);
//
// JSONObject jo=(JSONObject)JSONSerializer.toJSON(map);
//
// System.out.println(jo.get("age"));
// Person person=new Person("李萍萍",34,true);
// JSONObject jo=(JSONObject)JSONSerializer.toJSON(person);
//
// System.out.println(jo);
// List<Person> persons=new ArrayList<>();
//
// persons.add(new Person("李萍萍",34,true));
// persons.add(new Person("王萍萍",24,true));
// persons.add(new Person("张萍萍",31,false));
//
// JSONArray ja=(JSONArray)JSONSerializer.toJSON(persons);
// System.out.println(ja);
//from json to java
// JSONObject jo=new JSONObject();
// jo.element("name", "jack");
// jo.element("age", 23);
// jo.element("hasCar", false);
//
// //定义配置对象
// JsonConfig config=new JsonConfig();
// config.setRootClass(Person.class);
//
// Person person=(Person)JSONSerializer.toJava(jo,config);
// System.out.println(person.getName());
// JSONArray ja=new JSONArray();
// ja.add(new Person("李萍萍",34,true));
// ja.add(new Person("李冰冰",32,true));
// ja.add(new Person("李萍",31,false));
//
// JsonConfig config=new JsonConfig();
// //设置集合元素类型
// config.setRootClass(Person.class);
// //设置集合类型
// config.setArrayMode(JsonConfig.MODE_LIST);
//
// List<Person> persons=(List<Person>)JSONSerializer.toJava(ja, config);
//
// System.out.println(persons.get(0).getName());
Person person=new Person("李萍萍",34,true);
person.setBirthday(Date.valueOf("1999-08-09"));
JsonConfig config=new JsonConfig();
config.registerJsonValueProcessor(Date.class, new DateValueProcessor());
JSONObject jo=(JSONObject)JSONSerializer.toJSON(person,config);
System.out.println(jo);
}
}
