JAVA note 3 (Json analysis)

Jason concept:

The full English name is JavaScript Object Notation js object notation, which is a lightweight data exchange format.

Its presentation format

Object format:

Keys and values are connected by colons, and multiple key value pairs are separated by commas

The key of the key value pair should be enclosed in quotation marks (usually, when Java parsing, the key will report an error without quotation marks. JS can parse correctly.)

The value of a key value pair can be any type of data in JS

Examples:

                {

"name": "Golden Apple",

"info": "gold"

                }

An object is represented by a brace in which the attributes of the object are described in the form of key value pairs.

Array format:

In Json format, it can be nested with objects. You can write Json objects in square brackets separated by commas.

[element 1, element 2...]

Case

                {

                        "name":"hh",

"Friend": ["Zhang San", "Li Si", "Wang Wu", {"name": "pockmarked", "info": "good friend"}],

"info": "the most naughty in the class"

                }

JSON parsing

What needs to be done?

Converts a java object to a string in JSON format

Converts a string in JSON format to a java object

Here are two tools, gson and fastjason. Gson is owned by Google and fastjason is owned by Alibaba.

 Gson

To convert an object to a Json string.

           1. Introducing the jar package of gson

           2. Write the following code where you need to convert JSON strings:

                String json = new Gson(). Tojson (object to be converted);

        

public class Demo1 {
    public static void main(String[] args) {
        //1. Create a Gson object
        Gson gson = new Gson();
        //2. Conversion
        Book book = new Book("100","hhh","Crack me up");
        String bookJson = gson.toJson(book);
        System.out.println(bookJson);
    }
}

To convert a Json string to an object.

           1. Introducing the jar package of gson

           2. Write the following code where java objects need to be converted:

Object = new gson() Fromjson (JSON string, object type. class);

public class Demo2 {
    public static void main(String[] args) {
        //1. Create a Gson object
        Gson gson = new Gson();
        //2. Convert {"id":"100","name":"hhh","info": "laugh to death"}
        Book book = gson.fromJson("{\"id\":\"100\",\"name\":\"hhh\",\"info\":\"Crack me up\"}",Book.class);
        System.out.println(book.toString());
        //Convert to map
        HashMap data = gson.fromJson("{\"id\":\"100\",\"name\":\"hhh\",\"info\":\"Crack me up\",\"page\":[\"Hoe standing grain gradually pawning a midday\",\"Sweat drops under the grass\",\"Hey, hey, hey, hey\"]}",HashMap.class);
        List page = (List) data.get("page");
        System.out.println(page.get(1));
    }
}

FastJson

To convert an object to a Json string.

           1. Introducing the jar package of fastjson

           2. Write the following code where you need to convert JSON strings:

                String json = JSON. Tojsonstring (object to be converted);

        

public class Demo3 {
    public static void main(String[] args) {
        Book book = new Book("1002","hhhhhhhh","Laugh is over");
        //transformation
        String json = JSON.toJSONString(book);
        System.out.println(json);
    }
}

To convert a Json string to an object.

           1. Introducing the jar package of fastjson

           2. Write the following code where java objects need to be converted:

Object = JSON Parseobject (JSON string, object type. class);

public class Demo4 {
    public static void main(String[] args) {
        //transformation
        Book book = JSON.parseObject("{\"id\":\"1002\",\"info\":\"Laugh is over\",\"name\":\"hhhhhhhh\"}", Book.class);
        System.out.println(book);
    }
}

Keywords: Java JSON

Added by MetroidMaster1914 on Fri, 17 Dec 2021 00:34:43 +0200