XML and JSON (7) --- JSON parsing

1. Gson --- The use of Google's open source GSON

Open source address: https://github.com/google/gson

(1) Convert the object to a JSON string

Instructions:

1. Import JAR package

2. Write the following code where the JSON string needs to be converted:

String json = new Gson().toJSON(要转换的对象);

Book.java

 

package com.java.demo2;

import java.util.Objects;

/**
 * @author s
 * @version 1.0
 * @date 2022-02-21 11:48
 */
public class Book {
    private String id;
    private String name;
    private String info;

    public Book() {
    }

    public Book(String id, String name, String info) {
        this.id = id;
        this.name = name;
        this.info = info;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", info='" + info + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return Objects.equals(id, book.id) && Objects.equals(name, book.name) && Objects.equals(info, book.info);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, info);
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }
}

Demo1.java

package com.java.demo2;

import com.google.gson.Gson;

/**
 * @author s
 * @version 1.0
 * @date 2022-02-21 11:47
 */
public class Demo1 {
    public static void main(String[] args) {
        // 1.创建Gson对象
//        Gson g = new Gson();
//        // 2.转换
//        Book b = new Book("100","金苹果","种苹果真辛苦!");
//        String s = g.toJson(b);
//        System.out.println(s);
//        匿名对象
        Book b = new Book("100","金苹果","种苹果真辛苦!");
        String s = new Gson().toJson(b);
        System.out.println(s);
    }
}

 

(2) Convert JSON string to object

Instructions:

1. Import the JAR package;

2. Write the following code at the position where the Java object needs to be converted:

对象 = new Gson().fromJson(JSON字符串,对象类型.class);

Demo2.java

package com.java.demo2;

import com.google.gson.Gson;

/**
 * @author s
 * @version 1.0
 * @date 2022-02-21 13:49
 */
public class Demo2 {
    public static void main(String[] args) {
        // 1.创建Gson对象
//        Gson g = new Gson();
//        // 2.转换
//        Book b = g.fromJson("{\"id\":\"100\",\"name\":\"金苹果\",\"info\":\"种苹果真辛苦!\"}",Book.class);
//        System.out.println(b.getId());
//        匿名对象
        Book b = new Gson().fromJson("{\"id\":\"100\",\"name\":\"金苹果\",\"info\":\"种苹果真辛苦!\"}",Book.class);
        System.out.println(b.getId());
    }
}

Demo3.java

package com.java.demo2;

import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;

/**
 * @author s
 * @version 1.0
 * @date 2022-02-21 13:49
 */
public class Demo3 {
    public static void main(String[] args) {
        // 1.创建Gson对象
        Gson g = new Gson();
        // 2.转换 {"id":"100","name":"金苹果","info":"种苹果真辛苦!","page":["锄禾日当午","汗滴禾下土","哈哈哈哈"]}
        // 包含数组部分时,转换为list集合
        HashMap data = g.fromJson("{\"id\":\"100\",\"name\":\"金苹果\",\"info\":\"种苹果真辛苦!\",\"page\":[\"锄禾日当午\",\"汗滴禾下土\",\"哈哈哈哈\"]}", HashMap.class);
        List page = (List) data.get("page");
        System.out.println(page.get(1));
    }
}

2. FastJson -- the use of Ali's open source FastJson

(1) Convert the object to a JSON string

Instructions:

1. Import JAR package

2. Write the following code where the JSON string needs to be converted:

String json = JSON.toJSONString(要转换的对象);

(2) Convert JSON string to object

Instructions:

1. Import the JAR package;

2. Write the following code at the position where the Java object needs to be converted:

类型 对象名 = JSON.parseObject(JSON字符串,类型.class);

或

List<类型> list = JSON.parseArray(JSON字符串,类型.class);

Demo4.java

package com.java.demo2;

import com.alibaba.fastjson.JSON;

/**
 * @author s
 * @version 1.0
 * @date 2022-02-21 18:14
 */
public class Demo4 {
    public static void main(String[] args) {
        Book b = new Book("1002","唐诗三百首","唐诗");
        //  1.转换
        String json = JSON.toJSONString(b);
        System.out.println(json);
    }
}

Demo5.java

package com.java.demo2;

import com.alibaba.fastjson.JSON;

import java.util.List;

/**
 * @author s
 * @version 1.0
 * @date 2022-02-21 18:14
 */
public class Demo5 {
    public static void main(String[] args) {
        //  1.转换 {"id":"1002","info":"唐诗","name":"唐诗三百首"}
//        Book book = JSON.parseObject("{\"id\":\"1002\",\"info\":\"唐诗\",\"name\":\"唐诗三百首\"}",Book.class);
        // 2.转换 ["一二三","二三四","三四五"]
        List<String> book = JSON.parseArray("[\"一二三\",\"二三四\",\"三四五\"]",String.class);
        System.out.println(book.get(1));
    }
}

 

The road is long and long, I will search up and down!

Guess you like

Origin blog.csdn.net/weixin_38817361/article/details/123043171