JSON parsing
JSON: JavaScript Object Notation
JSON is a simple spectrum of JS objects and a lightweight data exchange format
A subset of the JS specification developed by the European Computer Association, which is completely independent of the programming language and uses text to represent data
Object format
Describe the above contents with java, js, XML and JSON:
a copy of books title brief introduction
java
class Book{ private String name; private String info; get/set... } Book b = new Book(); b.setName("Java core technology "); b.setInfo("Told Java Relevant contents of core technology"); ...
js
You can create objects directly without writing classes
Use the object directly Property name to create the property of the object
var b = new Object(); b.name = "Java core technology "; b.info = "Told Java Relevant contents of core technology" ;
XML
<book> <name>Java core technology </name> <info>Told Java Relevant contents of core technology</info> </book>
JSON
The quotation marks of name and info in JS can be omitted, either single or double. When interacting with Java programmers, name and info are quoted
{ "name":"Java core technology ", "info":"Told Java Relevant contents of core technology" }
A pair of braces represents an object, and braces contain key value pairs
The attribute of the object is described by key value pairs. The attribute name and attribute value are separated by colons: attribute name: attribute value
The key of the key value pair should be enclosed in quotation marks (JS can be parsed correctly, but usually when Java parses, the key will report an error without quotation marks)
The value of a key value pair can be any type of data in JS
Multiple attributes are separated by commas
In JSON format, it can be nested with objects: [element 1, element 2...]
Java and JSON
Quickly convert objects in Java into JSON format strings
Convert a string in JSON format into a Java object
GSON
From Google
Convert object to JSON string
step
-
Introduce JAR package
-
Write the following code where the JSON string needs to be converted:
String json = new Gson(). Tojson (object to be converted);
Code example:
public static void main(String[] args) { //1. Create a Gson object Gson g = new Gson(); //2. Conversion Book book = new Book(100,"Java core technology ","Told Java Relevant contents of core technology"); String s = g.toJson(book);//The incoming object is converted to the corresponding Json object System.out.println(s); }
Execution result:
{"id":100,"name":"Java core technology ","info":"Told Java Relevant contents of core technology"}
Convert JSON strings to objects
step
- Introduce JAR package
- Write the following code where Java objects need to be converted:
Object = new gson() Fromjason (JSON string, object type. class);
Code example 1 (back to object):
public static void main(String[] args) { //1. Create a Gson object Gson g = new Gson(); //2. Conversion {"id":100,"name":"Java core technology", "info": "about Java core technology"} Book book = g.fromJson("{\"id\":100,\"name\":\"Java core technology \",\"info\":\"Told Java Relevant contents of core technology\"}", Book.class); System.out.println(book.getId()); System.out.println(book.getName()); System.out.println(book.getInfo()); }
Execution result:
100 Java core technology Told Java Relevant contents of core technology
Code example 2 (roll back set):
public static void main(String[] args) { //1. Create a Gson object Gson g = new Gson(); //2. Conversion {"id":100,"name":"Java core technology", "info": "describes the relevant contents of Java core technology", "page": ["encapsulation", "inheritance", "polymorphism]} HashMap data = g.fromJson("{\"id\":100,\"name\":\"Java core technology \",\"info\":\"Told Java Relevant contents of core technology\",\"page\":[\"encapsulation\",\"inherit\",\"polymorphic\"]}", HashMap.class); System.out.println(data.get("id")); System.out.println(data.get("name")); System.out.println(data.get("info")); System.out.println(data.get("page")); System.out.println(data.get("page").getClass()); List page = (List)data.get("page"); System.out.println(page.get(1)); }
Execution result:
The array part of the object is converted to ArrayList during the conversion process
100.0 Java core technology Told Java Relevant contents of core technology [encapsulation, inherit, polymorphic] class java.util.ArrayList inherit
FastJson
From Ali
Convert object to JSON string
step
- Introduce JAR package
- Write the following code where the JSON string needs to be converted:
String json=JSON. Tojsonstring (object to be converted);
Code example:
public static void main(String[] args) { Book book = new Book(100,"Java core technology ","Told Java Relevant contents of core technology"); //1. Conversion String s = JSON.toJSONString(book); System.out.println(s); }
Execution result:
{"id":100,"info":"Told Java Relevant contents of core technology","name":"Java core technology "}
Convert JSON strings to objects
step
- Introduce JAR package
- Where Java objects need to be converted, write the following code:
Type object name = JSON Parseobject (JSON string, type. class);
or
List < type > List = JSON Parsearray (JSON string, type. class);
Code example 1 (back to object):
public static void main(String[] args) { //1. Conversion {"id":100,"info": "about Java core technology", "name":"Java core technology"} Book book = JSON.parseObject("{\"id\":100,\"info\":\"Told Java Relevant contents of core technology\",\"name\":\"Java core technology \"}", Book.class); System.out.println(book.getId()); System.out.println(book.getName()); System.out.println(book.getInfo()); }
Execution result:
100 Java core technology Told Java Relevant contents of core technology
Code example 2 (roll back set):
public static void main(String[] args) { //1. Conversion ["encapsulation", "inheritance", "polymorphism"] List<String> strings = JSON.parseArray("[\"encapsulation\",\"inherit\",\"polymorphic\"]", String.class); System.out.println(strings.get(1)); }
Execution result:
inherit