Compare the json

JSON technology research report of

a respective JSON technology introduction and the advantages and disadvantages
1.json-lib
json-lib beginning is the most widely used analytical tool json, json-lib a bad place really is dependent on many third-party packages ,
including commons-beanutils.jar, commons-collections- 3.2.jar, commons-lang-2.6.jar, commons-logging-1.1.1.jar, ezmorph-1.0.6.jar,
for the conversion of complex types, json- for bean json lib there converted into a defect, such as a class list which will set or map of another class, json-lib problem arises from bean to json conversion.
json-lib in function and performance of the above can not meet the needs of the Internet now.
2. Jackson open source
compared json-lib frame, Jackson jar package depends less easy to use and the performance should be relatively higher.
And Jackson community is relatively active, the speed is faster.
Jackson bean cause problems for complex json type of conversion, a collection of Map, List of the conversion problems.
Jackson For complex types of bean convert Json, format conversion Json json format instead of the standard
3.Google of Gson
Gson is the most versatile Json parsing artifact, Gson Initially, it was to cope with domestic demand while Google's own R & D evolved from Google ,
but since publicly released the first edition in May 2008 has been applied many companies or users.
Gson application mainly fromJson toJson with two conversion functions, no dependency, no additional JAR exceptions, can be run directly on the JDK.
But before using this type of object conversion you must first create a good object and its members will be successful successfully converted into the corresponding JSON string object.
As long as the class which get and set methods, can Gson complex type or bean to bean to json json conversion is JSON parsing artifact.
Gson in the above function impeccable, but the performance gap somewhat above than FastJson.
4. Ali Baba FASTJSON
FASTJSON Java language is written in a high-performance JSON processor, developed by Alibaba.
No dependence, no additional jar exception, can be directly run on JDK.
FastJson will appear on a complex type of Bean convert Json some of the problems, the type of reference may occur, resulting Json conversion error, the need for reference.
FastJson using original algorithm, we will parse the speed to the extreme, more than all the json library.

In summary comparison of four kinds Json technology, at the time the project selection can use Google's Gson and FastJson two kinds of Alibaba used in parallel,
if only functional requirements, there is no performance requirements, you can use the google Gson,
if the performance of the above Gson requirements can be used to convert json bean ensure proper data use json FastJson to convert bean
Second, the use of the Google Profile Gson package.
Gson class: the most basic json analytic tools
JsonParser categories: JsonElements parser to parse JSON to parse tree
JsonElement classes: one class represents an element of JSON
JsonObject categories: JSON object type
JsonArray categories: JsonObject array
TypeToken class: used to create a type, such as generic List <?>
(1) Maven relies

com.google.code.gson
GSON
2.2.4

(2) the basis of the conversion class
public class {Book
Private ID String;
Private String name;
public Book () {
Super ();
}
public String getId () {
return ID;
}
public void the setId (String ID) {
this.id ID =;
}
public String getName () {
return name;
}
public void the setName (String name) {
this.name = name;
}
}
public class Student {
Private String name;
Private int Age;
Private String Sex;
private String describe;
private Set books;
public Student() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Set getBooks() {
return books;
}
public void setBooks(Set books) {
this.books = books;
}
public String getDescribe() {
return describe;
}
Public void setDescribe (String DESCRIBE) {
this.describe = DESCRIBE;
}
}
(. 3) converting bean JSON
Gson GSON new new Gson = ();
String = gson.toJson JSON (obj);
obj is an object
(4) json conversion bean
= new new GSON GSON GSON ();
String JSON = "{\" ID \ ": \" 2 \ ", \" name \ ": \" Json technology \ "}";
Book Book = gson.fromJson (JSON, Book .class);
(. 5) converts the complex json bean, such as List, Set
the json converted into complex types of bean, requires TypeToken
Gson GSON new new Gson = ();
String json = "[{\" ID \ ": \ "1 \", \ "name \": \ "Json technology \"}, {\ "id \": \ "2 \", \ "name \": \ "java technology \"}] ";
// converted into json List
List List = gson.fromJson (json, new new TypeToken <The LIST> () {} .getType ());
// Set the converted json
= Gson.fromJson SET SET (json, new new TypeToken <the SET> () {} .getType ());
(. 6) directly operated by the object json json json and some tools
a) formatting Json
String json = "[{\ "id \": \ "1 \", \ "name \": \ "Json technology \"}, {\ "id \": \ "2 \", \ "name \": \ "java technology \" }] ";
Gson GSON new new GsonBuilder = () setPrettyPrinting () Create ();..
JsonParser new new JsonParser JP = ();
JsonElement JE = jp.parse (JSON);
JSON = gson.toJson (JE);
B) Analyzing whether the string is JSON, to judge whether the abnormality is captured by JSON
string JSON = "[{\" ID \ ": \". 1 \ ", \" name \ ": \" Json technology \ "}, {\ ' id \ ": \" 2 \ ", \" name \ ": \" java technology \ "}]";
Boolean jsonFlag;
the try {
. JsonParser new new () the parse (STR) .getAsJsonObject ();
= to true jsonFlag;
} the catch (Exception E) {
jsonFlag = to false;
}
C) acquired from the attribute string json
String json = "{\"id\":\"1\",\"name\":\"Json技术\"}";
String propertyName = 'id';
String propertyValue = "";
try {
JsonParser jsonParser = new JsonParser();
JsonElement element = jsonParser.parse(json);
JsonObject jsonObj = element.getAsJsonObject();
propertyValue = jsonObj.get(propertyName).toString();
} catch (Exception e) {
propertyValue = null;
}
d)除去json中的某个属性
String json = "{\"id\":\"1\",\"name\":\"Json技术\"}";
String propertyName = 'id';
JsonParser jsonParser = new JsonParser();
JsonElement element = jsonParser.parse(json);
JsonObject jsonObj = element.getAsJsonObject();
jsonObj.remove(propertyName);
= jsonObj.toString json ();
E) add attributes to the json
String json = "{\" id \ ": \" 1 \ ", \" name \ ": \" Json technology \ "}";
String propertyName = 'desc';
Object the propertyValue = "JSON various technical research";
jsonParser jsonParser new new jsonParser = ();
JsonElement = jsonParser.parse Element (JSON);
JSONObject jsonObj element.getAsJsonObject = ();
jsonObj.addProperty (propertyName, new new . GSON () the toJson (the propertyValue));
json = jsonObj.toString ();
F) to modify the properties of json
String json = "{\" id \ ": \" 1 \ ", \" name \ ": \" Json technology \ "}";
String propertyName = 'name';
Object the propertyValue = "JSON various technical research";
JsonParser jsonParser new new JsonParser = ();
JsonElement element = jsonParser.parse(json);
JsonObject jsonObj = element.getAsJsonObject();
jsonObj.remove(propertyName);
jsonObj.addProperty(propertyName, new Gson().toJson(propertyValue));
json = jsonObj.toString();
g)判断json中是否有属性
String json = "{\"id\":\"1\",\"name\":\"Json技术\"}";
String propertyName = 'name';
boolean isContains = false ;
JsonParser jsonParser = new JsonParser();
JsonElement element = jsonParser.parse(json);
JsonObject jsonObj = element.getAsJsonObject();
isContains = jsonObj.has(propertyName);
h)json中日期格式的处理
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Gson gson = builder.create();
Then treated gson json object if the object appears date Date class, will be processed in accordance formatted
I) in respect json Html escape
Gson gson = new Gson ();
This default objects to be transferred Html Meaning, if not escape using the following method
GsonBuilder new new GsonBuilder Builder = ();
builder.disableHtmlEscaping ();
Gson GSON Once builder.create = ();
three, Ali Baba FastJson use package profile.
(1) maven dependent

com.alibaba
FASTJSON
1.1.22

(2) Conversion base class
supra
(3) bean conversion json
convert the object into a formatted json
JSON.toJSONString (obj, to true);
the object into unformatted json
JSON.toJSONString (obj, to false);
obj design object
for complex type of conversion, repeated references to character references appear after the turn into json json string string, such as $ ref ":" $ [0 ] .books [. 1]
Student Student new new STU = ();
the Set Books = new new HashSet ();
Book Book Book new new = ();
books.add(book);
stu.setBooks(books);
List list = new ArrayList();
for(int i=0;i<5;i++)
list.add(stu);
String json = JSON.toJSONString(list,true);
(4)json转换bean
String json = "{\"id\":\"2\",\"name\":\"Json技术\"}";
Book book = JSON.parseObject(json, Book.class);
(5)json转换复杂的bean,比如List,Map
String json = "[{\"id\":\"1\",\"name\":\"Json技术\"},{\"id\":\"2\",\"name\":\"java技术\"}]";
//将json转换成List
List list = JSON.parseObject(json,new TypeReference<ARRAYLIST>(){});
//将json转换成Set
Set set = JSON.parseObject(json,new TypeReference<HASHSET>(){});String the propertyValue = "";String propertyName = 'ID';A) acquired from the attribute json string
(6) directly operated by the object json json



String json = "{\"id\":\"1\",\"name\":\"Json技术\"}";
JSONObject obj = JSON.parseObject(json);
propertyValue = obj.get(propertyName));
b)除去json中的某个属性
String propertyName = 'id';
String propertyValue = "";
String json = "{\"id\":\"1\",\"name\":\"Json技术\"}";
JSONObject obj = JSON.parseObject(json);
Set set = obj.keySet();
propertyValue = set.remove(propertyName);
json = obj.toString();
c)向json中添加属性
String propertyName = 'desc';
Object propertyValue = "json的玩意儿";
String json = "{\"id\":\"1\",\"name\":\"Json技术\"}";
JSONObject obj = JSON.parseObject(json);
obj.put(propertyName, JSON.toJSONString(propertyValue));
json = obj.toString();
d)修改json中的属性
String propertyName = 'name';
Object propertyValue = "json的玩意儿";
String json = "{\"id\":\"1\",\"name\":\"Json技术\"}";
JSONObject obj = JSON.parseObject(json);
Set set = obj.keySet();
if(set.contains(propertyName))
obj.put(propertyName, JSON.toJSONString(propertyValue));
json = obj.toString();
e)判断json中是否有属性
String propertyName = 'name';
boolean isContain = false;
String json = "{\"id\":\"1\",\"name\":\"Json技术\"}";
JSONObject obj = JSON.parseObject(json);
Set set = obj.keySet();
isContain = set.contains(propertyName);Object obj = new Date ();
f) Processing json date format

String json = JSON.toJSONStringWithDateFormat (obj, " yyyy-MM-dd HH: mm: ss.SSS");
use JSON.toJSONStringWithDateFormat, the method may be used to convert the date to the date formatting

three, json-lib package uses Introduction.
(1) maven dependent

net.sf.json-lib
JSON-lib
jdk15
2.2.2


Commons-BeanUtils
Commons-BeanUtils
1.8.3


Commons Collections-
Commons-Collections
3.2


Commons-lang
Commons-lang
2.6


Commons the logging- 
Commons the logging- 
1.1. . 1 


net.sf.ezmorph
ezmorph
1.0.6

(2) converted base class
supra
(3) bean conversion JSON
a) to convert to a class Json, obj is an ordinary object, not List, Map object
String json = JSONObject.fromObject ( obj) .toString ();
b) converting the List, Map into Json
String = JSONArray.fromObject JSON (List) .toString ();
String = JSONArray.fromObject JSON (Map) .toString ();
(. 4) JSON converting the bean
String JSON = "{\" id \ ": \" 1 \ ", \" name \ ": \" Json technology \ "}";
the JSONObject jsonObj = JSONObject.fromObject (JSON);
Book Book = (Book) JSONObject.toBean (jsonObj, a Book.class );
(. 5) JSON conversion List, for complex types of conversion problem arises
String json = "[{\" id \ ": \" 1 \ ", \" name \ ": \" Json technology \ "}, { \ "id \": \ " 2 \", \ "name \": \ "Java technology \"}] ";
the JSONArray JSONArray = JSONArray.fromObject (JSON);
the jSONObject jsonObject;
T the bean;
int size = jsonArray.size ();
List List the ArrayList new new = (size);
for (int i = 0; i < size; i++) {
jsonObject = jsonArray.getJSONObject(i);
= the bean (T) JSONObject.toBean (jsonObject, beanClass);
List.add (the bean);
}
(. 6) converts JSON the Map
String JSONString = "{\" ID \ ": \". 1 \ ", \" name \ " : \ "Json technology \"} ";
the jSONObject jsonObject = JSONObject.fromObject (JSONString);
the Iterator keyIter jsonObject.keys = ();
String Key;
Object value;
the Map = new new valueMap the HashMap ();
the while (keyIter.hasNext () ) {
Key = (String) keyIter.next ();
value = jsonObject.get (Key) .toString ();
valueMap.put (Key, value);
}
(. 7) JSON date for the operation of more complex and requires the use of JsonConfig than Gson and FastJson to be more trouble
interface class creates conversion, converted to the specified format date
class DateJsonValueProcessor implements JsonValueProcessor {
public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS"; 
private DateFormat dateFormat; 
public DateJsonValueProcessor(String datePattern) { 
try { 
dateFormat = new SimpleDateFormat(datePattern); 
} catch (Exception ex) { 
dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN); 


public Object processArrayValue(Object value, JsonConfig jsonConfig) { 
return process(value); 

public Object processObjectValue(String key, Object value, 
JsonConfig jsonConfig) { 
return process(value); 

private Object process(Object value) { 
return dateFormat.format[1];
Map<STRING,DATE> birthDays = new HashMap<STRING,DATE>();
birthDays.put("WolfKing",new Date());
JSONObject jsonObject = JSONObject.fromObject(birthDays, jsonConfig);
String json = jsonObject.toString();
System.out.println(json);
}
}
(8)JsonObject 对于json的操作和处理
a)从json串中获取属性
String jsonString = "{\"id\":\"1\",\"name\":\"Json技术\"}";
Object key = "name";
Object value = null;
JSONObject jsonObject = JSONObject.fromObject(jsonString);
value = jsonObject.get(key);
jsonString = jsonObject.toString();
b)除去json中的某个属性
String jsonString = "{\"id\":\"1\",\"name\":\"Json技术\"}";
Object key = "name";
= Null value Object;
the JSONObject jsonObject = JSONObject.fromObject (JSONString);
value = jsonObject.remove (Key);
JSONString jsonObject.toString = ();
C) and added to modify properties json, there is modified, no add
String jsonString = "{\" id \ ": \" 1 \ ", \" name \ ": \" Json technology \ "}";
Object Key = "desc";
Object value = "JSON good things";
the jSONObject jsonObject JSONObject.fromObject = (JSONString);
jsonObject.put (Key, value);
JSONString jsonObject.toString = ();
D) determining whether there json attribute
String jsonString = "{\" id \ ": \" 1 \ ", \ "name \": \ " Json technology \"} ";
Boolean = containFlag to false;
Object Key =" desc ";
the jSONObject = jsonObject the jSONObject.fromObject(jsonString);
containFlag = jsonObject.containsKey(key);

Guess you like

Origin www.cnblogs.com/xinglongbing521/p/11229650.html
Recommended