Introduction to JSON
The full name of JSON is JavaScript Object Notaion. It is a lightweight data exchange format with good readability and fast writing characteristics. In terms of text, it uses highly compatible text. The usage rate in the daily process is as high as 99%,
Data types supported by JSON
- Integer or floating point number;
- Boolean value (true, false);
- String
- Array
- function;
- Object
- null。
JSON basic syntax
- Object class
Use {} to contain key: Value key-value pairs, where Key and Value are separated by colons, and each key-value is separated by commas.
"Key1": "Value1", "Key2": "Value2"
- Array array class
Use [] to include all elements, each element is separated by a comma, the element can be any value
String arr1 = ["steamed bun", "winter melon", "eggplant",1,2,3]
- Combination
String arr2 = { "name" = "steamed bun", "name" = "winter melon", "eggplant" = {[1,2,3], [3,4,5]} }
JSON data analysis
Use json to convert
Book book = new Book("102","围城","讲述了一代人的爱恨情仇");
String json = JSON.toJSONString(book);
System.out.println(json);
Output value: {"id":"102","info":"tells the love and hatred of a generation","name":"siege"}
1. Analysis of Object Object
Book book = JSON.parseObject("{\"id\":\"102\",\"info\":\"讲述了一代人的爱恨情仇\",\"name\":\"围城\"}",Book.class);
System.out.println(book.getId());
System.out.println(book.getName());
System.out.println(book.getInfo());
The output value is:
102
Siege
tells about the love and hatred of a generation
2. Array analysis
List<String> list = JSON.parseArray("[\"馒头\",\"茄子\"]",String.class);
System.out.println(list.get(0));
System.out.println(list.get(1));
The output value is:
steamed buns
eggplant