fastjson与net.sf.json区别

 

   在现在的开发当中,

                   绝大多数引用阿里巴巴的fastjson。

                                              当然net.sf.json同样可以使用。

一、引入com.alibaba.fastjson包



      com.alibaba
      fastjson
      1.2.66

常用的格式转化:

Student stu = new Student("公众号编程大道", "m", 2);

//Java对象转化为JSON对象

JSONObject jsonObject = (JSONObject) JSONObject.toJSON(stu);

System.out.println("Java对象转化为JSON对象\n" + jsonObject);
//{"name":"公众号编程大道","age":2,"sex":"m"}

Student stu = new Student("公众号编程大道", "m", 2);

//Java对象转换成JSON字符串

String stuString = JSONObject.toJSONString(stu);

System.out.println("Java对象转换成JSON字符串\n" + stuString);
//{"age":2,"name":"公众号编程大道","sex":"m"}

Student stu = new Student("公众号编程大道", "m", 2);

//先转成JSON对象
 
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(stu);

//JSON对象转换为JSON字符串
        
String jsonString = jsonObject.toJSONString();
System.out.println("JSON对象转换为JSON字符串\n" + jsonString);

//{"name":"公众号编程大道","age":2,"sex":"m"}

Student stu = new Student("公众号编程大道", "m", 2);

//先转成JSON对象

JSONObject jsonObject = (JSONObject) JSONObject.toJSON(stu);
        
//JSON对象转换成Java对象

Student student = JSONObject.toJavaObject(jsonObject, Student.class);

System.out.println("JSON对象转换成Java对象\n" + student);
//Student{name='公众号编程大道', sex='m', age=2}

String stuString = "{\"age\":2,\"name\":\"公众号编程大道\",\"sex\":\"m\"}";

//JSON字符串转换成JSON对象

JSONObject jsonObject1 = JSONObject.parseObject(stuString);

System.out.println("JSON字符串转换成JSON对象\n" + jsonObject1);
//{"sex":"m","name":"公众号编程大道","age":2}

String stuString = "{\"age\":2,\"name\":\"公众号编程大道\",\"sex\":\"m\"}";

//JSON字符串转换成Java对象

Student student1 = JSONObject.parseObject(stuString, Student.class);

System.out.println("JSON字符串转换成Java对象\n" + student1);
//Student{name='公众号编程大道', sex='m', age=2}

更全面

//Object转json

JSONObject j1 = (JSONObject)JSONObject.toJSON(man1);

 //json转Object

 Man man3 = JSONObject.parseObject(j2.toJSONString(), Man.class);

 String aaa = "{\"name\":\"张三\",\"id\":\"1\"}";

 //String转JSONObject

  JSONObject j1 = JSONObject.parseObject(aaa);

 //JSONObject转String

  String bbb = j1.toJSONString(); 

 String aaa = "[{\"name\":\"张三\",\"id\":\"1\"},{\"name\":\"李四\",\"id\":\"2\"}]";

 //String转jsonArray

  JSONArray jsArr = JSONObject.parseArray(aaa);

 // jsonArray转String

  String bbb = jsArr.toJSONString();
  
 // list转jsonArray

 JSONArray array = JSONArray.parseArray(JSONObject.toJSONString(mans));

 // jsonArray转list

  List list = JSONObject.parseArray(js2.toJSONString(), Man.class);
 

 

 

 

二、引入net.sf.json包(解析json精度丢失


    net.sf.json-lib
    json-lib
    2.4
    jdk15

 

常用的格式转化:

 

string字符串-------》json数组

String json = “[{‘day1’:’work’,’day2’:26},{‘day1’:123,’day2’:26}]”;
JSONArray jsonArray = JSONArray.fromObject(json);
单个json对象转换

String aa = "{user:{name:\"张三\",age:\"20\"}}";
        
JSONObject obj = JSONObject.fromObject(aa);
List 转换成json(Map也可以)

List list = new ArrayList(); 
jb1.setCol(1); 
jb1.setRow(1); 
jb1.setValue("xx");

JSONArray ja = JSONArray.fromObject(list); 
遍历Json数组

输出每个成员

for(int i=0; i
获取每个成员的key及value

JSONObject obj = (JSONObject) jsonArray.get(i); 
Iterator it = obj.keys(); 
while (it.hasNext()) { 
  String key = it.next().toString(); 
  System.out.println("key ----- "+key); 
  System.out.println("value ----- "+obj.get(key)); 
} 
增加

JSONObject obj2 = new JSONObject(); 
obj2.put("day1", "study"); 
obj2.put("day2", "2"); 
jsonArray.add(obj2);
删除

jsonArray.remove(index); 
jsonArray.subList(fromIndex, toIndex)

 

你可能感兴趣的:(Json)