JSON format and use of Gson and FastJson


Introduction

JSON: JavaScrpit Object Notation JS object notation, is a lightweight interactive format. It is based on a subset of ECMAScript (the js specification developed by the European Computer Association), and uses a text format completely independent of the programming language to store and represent data. The concise and clear hierarchical structure makes JSON an ideal data exchange language. It is easy for people to read and write, but also easy for machine to parse and generate, and effectively improve network transmission efficiency


Object format

class Person{
    
    
	private String name;
	private String sex;
} 

JSON格式:
	JSON:{
    
    
		"name":"小明"
		"sex":"应该是男的"
	}
一个对象,由一个大括号表示.括号中描述对象的属性.通过键值对来描述对象的属性(可以理解为,大括号中,包含的是一个个的键值对.)
	格式:
		键与值之间使用冒号连接,多个键值对之间使用逗号分隔.
		键值对的键应使用引号引住(通常Java解析时,键不使用引号会报错.而JS能正确解析.)
		键值对的值,可以是JS中的任意类型的数据

Array format

在JSON格式中可以与对象互相嵌套[元素1,元素2...]

Case

The code is as follows (example):

{
    
    
	"name":"小明"
	"sex":"亲自验证是男的"
	"friend":["小红","小兰",{
    
    
		"name":"小黄"
		"info":"这个绝对是一个可爱的女孩子"
	}],
	"heihei":{
    
    
		"name":"大长刀",
		"length":"40m"}
}

Java given JSON

  • Quickly convert objects in Java into strings in JSON format.
  • Convert a string in JSON format into a Java object.

Gson

  • Convert object to JSON string
转换JSON字符串的步骤:
	1.引入JAR包
	2.在需要转换JSON字符串的位置编写如下代码即可:
		Stringjson=newGson().toJSON(要转换的对象);
 接下来我们来看案例

Book.java

public class Book {
    
    
    private String id;
    private String name;
    private String info;

    public Book(String id, String name, String info) {
    
    
        this.id = id;
        this.name = name;
        this.info = 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;
    }

    @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);
    }
}
 Book b=BookDao.find();
    String json=newGson().toJson(b);
    System.out.println(json);
  • Convert JSON object to object
1.引入JAR包
2.在需要转换Java对象的位置,编写如下代码:
	对象=newGson().fromJson(JSON字符串,对象类型.class);

Case:

 Gson g = new Gson();
        //转换    {"id":"100","name":"金苹果","info":"种植苹果真辛苦"}
        Book b = g.fromJson("{\"id\":\"100\",\"name\":\"金苹果\",\"info\":\"种植苹果真辛苦\"}",Book.class);
        System.out.println(b.getId());

FastJson

  • Convert object to JSON string
转换JSON字符串的步骤:
	1.引入JAR包
	2.在需要转换JSON字符串的位置编写如下代码即可:
	Stringjson=JSON.toJSONString(要转换的对象);
 Bookb=BookDao.find();
    Stringjson=JSON.toJSONString(b);
    System.out.println(json);
  • Convert JSON string to object
 1.引入JAR包
    2.在需要转换Java对象的位置,编写如下代码:
        类型对象名=JSON.parseObject(JSON字符串,类型.class);
            或
        List<类型>list=JSON.parseArray(JSON字符串,类型.class);
 //  转换  : {"id":"1002","info":"床前明月光,疑是地上霜。举头望明月,低头思故乡。","name":"唐诗三百首"}
        Book book = JSON.parseObject("{\"id\":\"1002\",\"info\":\"床前明月光,疑是地上霜。举头望明月,低头思故乡。\",\"name\":\"唐诗三百首\"}",Book.class);
        System.out.println(book.getId());

Guess you like

Origin blog.csdn.net/weixin_43515837/article/details/111145676