The difference com.alibaba.fastjson and net.sf.json

JSON there are two structures

json simply is javascript objects and arrays, so the two structures is the object and an array of two structures, two structures can be represented by a variety of complex structures

1 , the object: object js expressed as "{}" enclosed content, the data structure is  {Key : value, key : value, ...} of the structure of key-value pairs, in object-oriented languages, Key property of the object, the value of the corresponding property value, so it is readily understood that the value of the method the object .key  getting a property value, the type of the attribute values may be numbers, strings, arrays, several objects.

2 , the array: array js is in brackets "[]" enclosed content, the data structure  [ "Java", "JavaScript", "VB", ...] , the values in the same way and all languages, obtaining an index, the type of field values may be numbers, strings, arrays, several objects. After the object array 2 kind of structure can be combined into a complex data structure.

 

First, the use fastjson

1, the mvn ( https://mvnrepository.com ) library is introduced

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>

2, fastjson api

public static final Object parse (String text); // parse the string as JSON or JSONObject JSONArray 

 


public static final JSONObject parseObject (String text); // parse the string into JSON JSONObject, noted, json json string objects are not the same


public static final <T> T parseObject (String text, Class <T> clazz); // parse the string to JSON JavaBean 


public static final JSONArray parseArray (String text); // parse the string into JSON JSONArray 


public static final <T> List <T> parseArray (String text, Class <T> clazz); // parse the string into JavaBean set JSON 


public static final String toJSONString (Object object); // a JavaBean sequence string as JSON


public static final String toJSONString (Object object, boolean prettyFormat); // a JavaBean sequence string formatted as JSON


public static final Object toJSON (Object javaObject); JSONObject or converted to a JavaBean JSONArray.

 

Case:

 

Test category

package com.sanyouhome.json;

import java.util.List;

class Hobby{
private Long itemId;
private String itemName;

public Long getItemId() {
return itemId;
}

public void setItemId(Long itemId) {
this.itemId = itemId;
}

public String getItemName() {
return itemName;
}

public void setItemName(String itemName) {
this.itemName = itemName;
}
}
public class Boss {
private Long id;
private String name;

private List<Hobby> hobby;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<Hobby> getHobby() {
return hobby;
}

public void setHobby(List<Hobby> hobby) {
this.hobby = hobby;
}

@Override
public String toString() {
return "Boss{" +
"id=" + id +
", name='" + name + '\'' +
", hobby=" + hobby +
'}';
}
}

 

testing method

// define json String 
String jsonStr = "{\" id \ ": \" 10 \ ", \" name \ ": \" chrchr \ ", \" hobby \ ": [{\" itemId \ ": \ "20 \", \ "itemName \": \ " basketball \"}, {\ "itemId \": \ "21 \", \ "itemName \": \ " swimming \"}]} ";

// will json json string converted into an object, the parseObject
the jSONObject jsonObject = JSON.parseObject (jsonStr);
//System.out.println (jsonObject.toString ());

// the json object into a java object, toJavaObject, the parameter [1 json Object], parameters [2] javaBean
Boss BOSS1 = JSON.toJavaObject (jsonObject, Boss.class);
//System.out.println (boss1.toString ());

// the character string converted into json java object, the parseObject, json string parameter [1], the parameter [2] javaBean
Boss BOSS2 = JSON.parseObject (jsonStr, Boss.class);
//System.out.println (boss2.toString ());

Boss boss3 = new Boss();
boss3.setId(1L);
boss3.setName("chrchr");
= New new Hobby Hobby Hobby ();
hobby.setItemId (30L);
hobby.setItemName ( "lanqiu");
the ArrayList <Hobby> = new new hobbies the ArrayList <> ();
hobbies.add (Hobby);
boss3.setHobby (hobbies) ;

// java object into a json the objects, the toJSONString
the JSONObject = O (the JSONObject) JSON.toJSON (BOSS3);
//System.out.println(o);

// java object will be transferred into json string, the toJSONString
string S JSONObject.toJSONString = (BOSS3);
//System.out.println(s);

// java the object into a string json, the toJSONString
string the JSONObject new new S1 = () the toJSONString (BOSS3);.

// Get the object json a certain value, the getString
String name = jsonObject.getString ( "name");
System.out.println (name);

 

JSONObject which is inherited JSON achieve a Map <String, Object>, are used in the method Map and. It can be said JSONObject equivalent Map <String, Object>

JSONObject extends JSON implements Map<String, Object>

JSONArray is inherited JSON realize List <Object>, List and method are used in. It can be said JSONArray the equivalent of List <Object>

JSONArray extends JSON implements List<Object>



3, using net.sf.json.JSONObject

// json string, java objects, map objects converted to JSONObject

JSONObject.fromObject(hashMap)

// json object is converted into a java object, and get the java object properties 

JSONObject.toBean(jsonStu, Student.class); 

jsonObj.toString()

 

// json string array, list the set of objects converted to an array type JSONArray

JSONArray.fromObject(arrayList)

 

 

 

 

Guess you like

Origin www.cnblogs.com/caohanren/p/11668292.html