Jack Son's Learning Notes (I)

Summary

Jackson framework is a set of data processing tools based on Java platform, which is called "the best Java Json parser".  
The Jackson framework contains three core libraries: streaming, databind and annotations. Jackson also contains other data processing class libraries, which are not explained.
Jackson version: 1.x (current version 1.1-1.9) and 2.x. 1. X and 2. x can be seen from the naming of packages. In the class library of 1. x, the package name begins with: org. codehaus. jackson. x xxx, while in the class library of 2. x, the command begins with: com. fastxml. jackson. xxxx.

Jackson Home Page: https://github.com/FasterXML/jackson
Jackson Wiki: http://wiki.fasterxml.com/JacksonHome
Jackson doc: https://github.com/FasterXML/jackson-docs
Jackson Download Page: http://wiki.fasterxml.com/JacksonDownload


Dead work

All the programs in this paper are based on JDK 1.7 and depend on three core class libraries of jackon:
jackson-core-2.5.3.jar
jackson-annotations-2.5.3.jar
jackson-databind-2.5.3.jar


Jackson handles Json

Jackson provides three alternative Json processing methods: Streaming API, Tree Model, and Data Binding. From a usage point of view, compare the characteristics of these three ways of dealing with Json:

Streaming API: The most efficient way to process (low overhead, fast read and write, but high programming complexity)
Tree Model: The most flexible approach
Data Binding: The most commonly used method of processing

Next, we create and parse Json strings by using DataBinding, TreeModel and Streaming, respectively.

1. Data Binding handles Json

Jackson supports the transformation between Java objects and Json. Java objects can be serialized into JSON strings, and JSON strings can also be deserialized into the same Java objects.

(1)java objects are converted to json:
Province.java
  1. package com.jackson.json.databinding;  
  2.   
  3. public class Province {  
  4.     public String name;  
  5.     public int population;  
  6.     public String[] city;     
  7. }  
Country.java
  1. package com.jackson.json.databinding;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Arrays;  
  5. import java.util.Date;  
  6. import java.util.HashMap;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9.   
  10. public class Country {  
  11.     //Note: private property fields of serialized bean s need to create getter methods or property fields should be public  
  12.     private String country_id;  
  13.     private Date birthDate;  
  14.     private List<String> nation = new ArrayList<String>();  
  15.     private String[] lakes;  
  16.     private List<Province> provinces = new ArrayList<Province>();  
  17.     private Map<String, Integer> traffic = new HashMap<String, Integer>();  
  18.   
  19.     public Country() {  
  20.         // TODO Auto-generated constructor stub  
  21.     }  
  22.   
  23.     public Country(String countryId) {  
  24.         this.country_id = countryId;  
  25.     }  
  26.   
  27.     public String getCountry_id() {  
  28.         return country_id;  
  29.     }  
  30.   
  31.     public void setCountry_id(String country_id) {  
  32.         this.country_id = country_id;  
  33.     }  
  34.   
  35.     public Date getBirthDate() {  
  36.         return birthDate;  
  37.     }  
  38.   
  39.     public void setBirthDate(Date birthDate) {  
  40.         this.birthDate = birthDate;  
  41.     }  
  42.   
  43.     public List<String> getNation() {  
  44.         return nation;  
  45.     }  
  46.   
  47.     public void setNation(List<String> nation) {  
  48.         this.nation = nation;  
  49.     }  
  50.   
  51.     public String[] getLakes() {  
  52.         return lakes;  
  53.     }  
  54.   
  55.     public void setLakes(String[] lakes) {  
  56.         this.lakes = lakes;  
  57.     }  
  58.   
  59.     public Integer get(String key) {  
  60.         return traffic.get(key);  
  61.     }  
  62.   
  63.     public Map<String, Integer> getTraffic() {  
  64.         return traffic;  
  65.     }  
  66.   
  67.     public void setTraffic(Map<String, Integer> traffic) {  
  68.         this.traffic = traffic;  
  69.     }  
  70.   
  71.     public void addTraffic(String key, Integer value) {  
  72.         traffic.put(key, value);  
  73.     }  
  74.   
  75.     public List<Province> getProvinces() {  
  76.         return provinces;  
  77.     }  
  78.   
  79.     public void setProvinces(List<Province> provinces) {  
  80.         this.provinces = provinces;  
  81.     }  
  82.   
  83.     @Override  
  84.     public String toString() {  
  85.         return "Country [country_id=" + country_id + ", birthDate=" + birthDate  
  86.                 + ", nation=" + nation + ", lakes=" + Arrays.toString(lakes)  
  87.                 + ", province=" + provinces + ", traffic=" + traffic + "]";  
  88.     }  
  89.   
  90. }  
JavaBeanSerializeToJson.java
  1. package com.jackson.json.databinding;  
  2.   
  3. import java.io.File;  
  4. import java.text.SimpleDateFormat;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. import com.fasterxml.jackson.annotation.JsonInclude.Include;  
  9. import com.fasterxml.jackson.databind.ObjectMapper;  
  10. import com.fasterxml.jackson.databind.SerializationFeature;  
  11.   
  12. public class JavaBeanSerializeToJson {  
  13.   
  14.     public static void convert() throws Exception {  
  15.         //Use ObjectMapper to convert objects to Json  
  16.         ObjectMapper mapper = new ObjectMapper();  
  17.         //Adding functionality to make the time format more readable  
  18.         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
  19.         mapper.setDateFormat(dateFormat);  
  20.   
  21.         Country country = new Country("China");  
  22.         country.setBirthDate(dateFormat.parse("1949-10-01"));  
  23.         country.setLakes(new String[] { "Qinghai Lake""Poyang Lake",  
  24.                 "Dongting Lake""Taihu Lake" });  
  25.   
  26.         List<String> nation = new ArrayList<String>();  
  27.         nation.add("Han");  
  28.         nation.add("Meng");  
  29.         nation.add("Hui");  
  30.         nation.add("WeiWuEr");  
  31.         nation.add("Zang");  
  32.         country.setNation(nation);  
  33.   
  34.         Province province = new Province();  
  35.         province.name = "Shanxi";  
  36.         province.population = 37751200;  
  37.         Province province2 = new Province();  
  38.         province2.name = "ZheJiang";  
  39.         province2.population = 55080000;  
  40.         List<Province> provinces = new ArrayList<Province>();  
  41.         provinces.add(province);  
  42.         provinces.add(province2);  
  43.         country.setProvinces(provinces);  
  44.           
  45.         country.addTraffic("Train(KM)"112000);  
  46.         country.addTraffic("HighWay(KM)"4240000);  
  47.         //To make JSON visually readable, add the following line of code. Note that this is not necessary in production because it will increase the content of Json  
  48.         mapper.configure(SerializationFeature.INDENT_OUTPUT, true);  
  49.         //Configure mapper to ignore empty attributes  
  50.         mapper.setSerializationInclusion(Include.NON_EMPTY);  
  51.         //By default, Jackson uses the Java property field name as the Json property name, or Jackson annotations to change the Json property name  
  52.         mapper.writeValue(new File("country.json"), country);  
  53.     }  
  54.   
  55.     public static void main(String[] args) throws Exception {  
  56.         convert();  
  57.     }  
  58.   
  59. }  
After the program runs, country.json is generated, which is as follows:
  1. {  
  2.   "country_id" : "China",  
  3.   "birthDate" : "1949-10-01",  
  4.   "nation" : [ "Han", "Meng", "Hui", "WeiWuEr", "Zang" ],  
  5.   "lakes" : [ "Qinghai Lake", "Poyang Lake", "Dongting Lake", "Taihu Lake" ],  
  6.   "provinces" : [ {  
  7.     "name" : "Shanxi",  
  8.     "population" : 37751200  
  9.   }, {  
  10.     "name" : "ZheJiang",  
  11.     "population" : 55080000  
  12.   } ],  
  13.   "traffic" : {  
  14.     "HighWay(KM)" : 4240000,  
  15.     "Train(KM)" : 112000  
  16.   }  
  17. }  

(2) Deserializing Json strings into java objects:
  1. package com.jackson.json.databinding;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.text.SimpleDateFormat;  
  6. import java.util.Iterator;  
  7. import java.util.List;  
  8.   
  9. import com.fasterxml.jackson.core.JsonParseException;  
  10. import com.fasterxml.jackson.databind.DeserializationFeature;  
  11. import com.fasterxml.jackson.databind.JsonMappingException;  
  12. import com.fasterxml.jackson.databind.ObjectMapper;  
  13.   
  14. /** 
  15.  * Deserializing Json strings into Java objects 
  16.  */  
  17. public class JsonDeserializeToJava {  
  18.       
  19.     public static void main(String[] args) throws Exception {  
  20.         //ObjectMapper class with serialization and deserialization mappers  
  21.         ObjectMapper mapper = new ObjectMapper();  
  22.         File json = new File("country.json");  
  23.         //When json is deserialized, the deserialization caused by unknown attributes is interrupted. Here we disable the deserialization function by interrupting unknown attributes.  
  24.         //Because, for example, there are 10 attributes in json, while only two attributes are defined in our bean, the other eight attributes will be ignored.  
  25.         mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);  
  26.           
  27.         //Mapping from json to java object, you can traverse the country object after you get it. Here's a part of the traversal, you can explain the problem.  
  28.         Country country = mapper.readValue(json, Country.class);  
  29.         System.out.println("country_id:"+country.getCountry_id());  
  30.         //Set time format for easy reading  
  31.         SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");  
  32.         String birthDate = dateformat.format(country.getBirthDate());  
  33.         System.out.println("birthDate:"+birthDate);  
  34.           
  35.         List<Province> provinces = country.getProvinces();  
  36.         for (Province province : provinces) {  
  37.             System.out.println("province:"+province.name + "\n" + "population:"+province.population);  
  38.         }  
  39.     }  
  40. }  
The results of program operation are as follows:
  1. country_id:China  
  2. birthDate:1949-10-01  
  3. province:Shanxi  
  4. population:37751200  
  5. province:ZheJiang  
  6. population:55080000  

2.Tree Model Processing Json

(1)tree model generates json:

  1. package com.jackson.json.treemodel;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileWriter;  
  5.   
  6. import com.fasterxml.jackson.core.JsonFactory;  
  7. import com.fasterxml.jackson.core.JsonGenerator;  
  8. import com.fasterxml.jackson.databind.ObjectMapper;  
  9. import com.fasterxml.jackson.databind.SerializationFeature;  
  10. import com.fasterxml.jackson.databind.node.ArrayNode;  
  11. import com.fasterxml.jackson.databind.node.JsonNodeFactory;  
  12. import com.fasterxml.jackson.databind.node.ObjectNode;  
  13.   
  14. public class SerializationExampleTreeModel {  
  15.       
  16.     public static void main(String[] args) throws Exception {  
  17.         //Create a node factory to provide us with all nodes  
  18.         JsonNodeFactory factory = new JsonNodeFactory(false);  
  19.         //Create a json factory to write tree modle as json  
  20.         JsonFactory jsonFactory = new JsonFactory();  
  21.         //Create a json generator  
  22.         JsonGenerator generator = jsonFactory.createGenerator(new FileWriter(new File("country2.json")));  
  23.         //Note that by default, the object mapper does not specify the root node, which is set as country below.  
  24.         ObjectMapper mapper = new ObjectMapper();  
  25.         ObjectNode country = factory.objectNode();  
  26.           
  27.         country.put("country_id""China");  
  28.         country.put("birthDate""1949-10-01");  
  29.           
  30.         //In Java, when List and Array are converted to json, the corresponding format symbols are "obj:[]"  
  31.         ArrayNode nation = factory.arrayNode();  
  32.         nation.add("Han").add("Meng").add("Hui").add("WeiWuEr").add("Zang");  
  33.         country.set("nation", nation);  
  34.           
  35.         ArrayNode lakes = factory.arrayNode();  
  36.         lakes.add("QingHai Lake").add("Poyang Lake").add("Dongting Lake").add("Taihu Lake");  
  37.         country.set("lakes", lakes);  
  38.           
  39.         ArrayNode provinces = factory.arrayNode();  
  40.         ObjectNode province = factory.objectNode();  
  41.         ObjectNode province2 = factory.objectNode();  
  42.         province.put("name","Shanxi");  
  43.         province.put("population"37751200);  
  44.         province2.put("name","ZheJiang");  
  45.         province2.put("population"55080000);  
  46.         provinces.add(province).add(province2);  
  47.         country.set("provinces", provinces);  
  48.           
  49.         ObjectNode traffic = factory.objectNode();  
  50.         traffic.put("HighWay(KM)"4240000);  
  51.         traffic.put("Train(KM)"112000);  
  52.         country.set("traffic", traffic);  
  53.           
  54.         mapper.configure(SerializationFeature.INDENT_OUTPUT, true);  
  55.         mapper.writeTree(generator, country);  
  56.     }  
  57.   
  58. }  

The program runs and generates country2.json, which reads as follows:

  1. {"country_id":"China","birthDate":"1949-10-01","nation":["Han","Meng","Hui","WeiWuEr","Zang"],"lakes":["QingHai Lake","Poyang Lake","Dongting Lake","Taihu Lake"],"provinces":[{"name":"Shanxi","population":37751200},{"name":"ZheJiang","population":55080000}],"traffic":{"HighWay(KM)":4240000,"Train(KM)":112000}}  

(2) json string deserialization to tree mode

Deserialization ExampleTreeModel 1. java, notice the type changes of different JsonNode s in the program

  1. package com.jackson.json.treemodel;  
  2.   
  3. import java.io.File;  
  4. import java.util.Iterator;  
  5.   
  6. import com.fasterxml.jackson.databind.JsonNode;  
  7. import com.fasterxml.jackson.databind.ObjectMapper;  
  8.   
  9. public class DeserializationExampleTreeModel1 {  
  10.   
  11.     public static void main(String[] args) throws Exception {  
  12.         ObjectMapper mapper = new ObjectMapper();  
  13.         //Jackson provides a tree node called "JsonNode", and ObjectMapper provides a way to read json as the tree's JsonNode root node  
  14.         JsonNode node = mapper.readTree(new File("country2.json"));  
  15.         //Look at the type of root node  
  16.         System.out.println("node JsonNodeType:"+node.getNodeType());  
  17.         //Is it a container?  
  18.         System.out.println("node is container Node ? "+node.isContainerNode());  
  19.         //Get the child node names of all node nodes  
  20.         System.out.println("---------Get all node Name of child node of node-------------------------");  
  21.         Iterator<String> fieldNames = node.fieldNames();  
  22.         while (fieldNames.hasNext()) {  
  23.             String fieldName = fieldNames.next();  
  24.             System.out.print(fieldName+" ");  
  25.         }  
  26.         System.out.println("\n-----------------------------------------------------");  
  27.         //The function of as.Text is to return a value to an empty string and a value to an empty string.  
  28.         JsonNode country_id = node.get("country_id");  
  29.         System.out.println("country_id:"+country_id.asText() + " JsonNodeType:"+country_id.getNodeType());  
  30.           
  31.         JsonNode birthDate = node.get("birthDate");  
  32.         System.out.println("birthDate:"+birthDate.asText()+" JsonNodeType:"+birthDate.getNodeType());  
  33.           
  34.         JsonNode nation = node.get("nation");  
  35.         System.out.println("nation:"+ nation+ " JsonNodeType:"+nation.getNodeType());  
  36.           
  37.         JsonNode lakes = node.get("lakes");  
  38.         System.out.println("lakes:"+lakes+" JsonNodeType:"+lakes.getNodeType());  
  39.   
  40.         JsonNode provinces = node.get("provinces");  
  41.         System.out.println("provinces JsonNodeType:"+provinces.getNodeType());  
  42.   
  43.         boolean flag = true;  
  44.         for (JsonNode provinceElements : provinces) {  
  45.             //In order to avoid provinceElements printing many times, using flag to control printing, can reflect the provinceElements JsonNodeType.  
  46.             if(flag){  
  47.                 System.out.println("provinceElements JsonNodeType:"+provinceElements.getNodeType());  
  48.                 System.out.println("provinceElements is container node? "+provinceElements.isContainerNode());  
  49.                 flag = false;  
  50.             }  
  51.             Iterator<String> provinceElementFields = provinceElements.fieldNames();  
  52.             while (provinceElementFields.hasNext()) {  
  53.                 String fieldName = (String) provinceElementFields.next();  
  54.                 String province;  
  55.                 if ("population".equals(fieldName)) {  
  56.                     province = fieldName + ":" + provinceElements.get(fieldName).asInt();  
  57.                 }else{  
  58.                     province = fieldName + ":" + provinceElements.get(fieldName).asText();  
  59.                 }  
  60.                 System.out.println(province);  
  61.             }  
  62.         }  
  63.     }  
  64. }  

After running the program, the printing results are as follows:

  1. node JsonNodeType:OBJECT  
  2. node is container Node ? true  
  3. ---------Get all node Name of child node of node-------------------------  
  4. country_id birthDate nation lakes provinces traffic   
  5. -----------------------------------------------------  
  6. country_id:China JsonNodeType:STRING  
  7. birthDate:1949-10-01 JsonNodeType:STRING  
  8. nation:["Han","Meng","Hui","WeiWuEr","Zang"] JsonNodeType:ARRAY  
  9. lakes:["QingHai Lake","Poyang Lake","Dongting Lake","Taihu Lake"] JsonNodeType:ARRAY  
  10. provinces JsonNodeType:ARRAY  
  11. provinceElements JsonNodeType:OBJECT  
  12. provinceElements is container node? true  
  13. name:Shanxi  
  14. population:37751200  
  15. name:ZheJiang  
  16. population:55080000  


Let's look at Deserialization ExampleTreeModel 2. java. In this case, we use the JsonNode.path method, which is similar to the get method used in Deserialization ExampleTreeModel 1. java.

But when the node does not exist, the get method returns null, while the path returns JsonNode of MISSING type.

  1. package com.jackson.json.treemodel;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.Iterator;  
  6.   
  7. import com.fasterxml.jackson.core.JsonProcessingException;  
  8. import com.fasterxml.jackson.databind.JsonNode;  
  9. import com.fasterxml.jackson.databind.ObjectMapper;  
  10.   
  11. public class DeserializationExampleTreeModle2 {  
  12.       
  13.     public static void main(String[] args) throws JsonProcessingException, IOException{  
  14.         ObjectMapper mapper = new ObjectMapper();  
  15.         JsonNode node = mapper.readTree(new File("country2.json"));  
  16.         //When the path method obtains JsonNode, it returns JsonNode of MISSING type when the object does not exist.  
  17.         JsonNode missingNode = node.path("test");  
  18.         if(missingNode.isMissingNode()){  
  19.             System.out.println("JsonNodeType : " + missingNode.getNodeType());  
  20.         }  
  21.   
  22.         System.out.println("country_id:"+node.path("country_id").asText());  
  23.           
  24.         JsonNode provinces = node.path("provinces");  
  25.         for (JsonNode provinceElements : provinces) {  
  26.             Iterator<String> provincesFields = provinceElements.fieldNames();  
  27.             while (provincesFields.hasNext()) {  
  28.                 String fieldName = (String) provincesFields.next();  
  29.                 String province;  
  30.                 if("name".equals(fieldName)){  
  31.                     province = fieldName +":"+ provinceElements.path(fieldName).asText();  
  32.                 }else{  
  33.                     province = fieldName +":"+ provinceElements.path(fieldName).asInt();  
  34.                 }  
  35.                 System.out.println(province);  
  36.             }  
  37.         }  
  38.     }  
  39.   
  40. }  
The program runs and prints the result:
  1. JsonNodeType : MISSING  
  2. country_id:China  
  3. name:Shanxi  
  4. population:37751200  
  5. name:ZheJiang  
  6. population:55080000  

3.Stream handles Json

(1)stream generation json

  1. package com.jackson.json.streaming;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileWriter;  
  5. import java.io.Exception;  
  6.   
  7. import com.fasterxml.jackson.core.JsonFactory;  
  8. import com.fasterxml.jackson.core.JsonGenerator;  
  9.   
  10. public class StreamGeneratorJson {  
  11.       
  12.     public static void main(String[] args) throws Exception {  
  13.         JsonFactory factory = new JsonFactory();  
  14.         //Create an instance of a JsonGenerator generator from JsonFactory  
  15.         JsonGenerator generator = factory.createGenerator(new FileWriter(new File("country3.json")));  
  16.           
  17.         generator.writeStartObject();  
  18.         generator.writeFieldName("country_id");  
  19.         generator.writeString("China");  
  20.         generator.writeFieldName("provinces");  
  21.         generator.writeStartArray();  
  22.         generator.writeStartObject();  
  23.         generator.writeStringField("name""Shanxi");  
  24.         generator.writeNumberField("population"33750000);  
  25.         generator.writeEndObject();  
  26.         generator.writeEndArray();  
  27.         generator.writeEndObject();  
  28.           
  29.         generator.close();  
  30.     }  
  31.   
  32. }  
After the program runs, country3.json file content is generated:
  1. {"country_id":"China","provinces":[{"name":"Shanxi","population":33750000}]}  

(2)stream parsing json:
Now adgcountry3.json, we use the Streaming API to parse the above Json and find the value of population in json.
  1. package com.jackson.json.streaming;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5.   
  6. import com.fasterxml.jackson.core.JsonFactory;  
  7. import com.fasterxml.jackson.core.JsonParseException;  
  8. import com.fasterxml.jackson.core.JsonParser;  
  9. import com.fasterxml.jackson.core.JsonToken;  
  10.   
  11. /*Jackson API Provides token for each Json object. For example, the Json start symbol "{" is the first object to be parsed by token. 
  12.  key:value The key-value pair is another separate object. This API is powerful, but it also requires a lot of coding. It's not recommended. Usually it's more about using Data Binding and TreeModel to handle json. 
  13.  */  
  14. public class StreamParserJson {  
  15.     public static void main(String[] args) throws JsonParseException,  
  16.             IOException {  
  17.         JsonFactory factory = new JsonFactory();  
  18.         //Create an instance of a JsonParser parser from JsonFactory  
  19.         JsonParser parser = factory.createParser(new File("country3.json"));  
  20.   
  21.         while (!parser.isClosed()) {  
  22.             //Get a token that points to the first symbol "{" in the json file on the first traversal  
  23.             JsonToken token = parser.nextToken();  
  24.             if (token == null) {  
  25.                 break;  
  26.             }  
  27.             //We just look for the value of the "population" field in country3.json to reflect the parsing process.  
  28.             //When the key is provinces, we go into provinces and find population.  
  29.             if (JsonToken.FIELD_NAME.equals(token)  
  30.                     && "provinces".equals(parser.getCurrentName())) {  
  31.                 token = parser.nextToken();  
  32.                 if (!JsonToken.START_ARRAY.equals(token)) {  
  33.                     break;  
  34.                 }  
  35.                 //At this point, token should point to "{"  
  36.                 token = parser.nextToken();  
  37.                 if (!JsonToken.START_OBJECT.equals(token)) {  
  38.                     break;  
  39.                 }  
  40.                 while (true) {  
  41.                     token = parser.nextToken();  
  42.                     if (token == null) {  
  43.                         break;  
  44.                     }  
  45.                     if (JsonToken.FIELD_NAME.equals(token)  
  46.                             && "population".equals(parser.getCurrentName())) {  
  47.                         token = parser.nextToken();  
  48.                         System.out.println(parser.getCurrentName() + " : "  
  49.                                 + parser.getIntValue());  
  50.                     }  
  51.                 }  
  52.             }  
  53.         }  
  54.     }  
  55.   
  56. }  
After the program runs, the results are printed in the console as follows:
  1. population : 33750000  

summary

In the example above, I have dealt with Json in three ways. My experience is as follows:

Stream API has the lowest overhead and the highest efficiency, but it also has the highest coding complexity. When generating Json, we need to write symbols and field splicing JSON step by step. When parsing Json, we need to find JSON values according to token pointing. It is not convenient to generate and parse json, and the code readability is also low.
Data binding is the most commonly used method of JSON processing. When generating json, we create relevant java objects, assemble them according to JSON content structure, and finally call the writeValue method to generate json.
When parsing, it is simpler to map json directly to the relevant java objects, and then you can traverse the java objects to get the values.
TreeModel deals with Json. It generates and parses JSON by tree structure. When generating json, according to JSON content structure, we create different types of node objects and assemble these nodes to generate json. When parsing json, it does not need to bind JSON to Java beans. According to JSON structure, it can easily find content by path or get method.

Learning reference: http://www.cnblogs.com/lee0oo0/articles/2652528.html

Keywords: JSON Java github JDK

Added by SensualSandwich on Wed, 22 May 2019 22:52:16 +0300