解析JSON简介

解析JSON

简介

通常使用的工具有Gson、FastJson,可实现JSON格式的String与Java对象之间的转换。

导入jar包

在这里插入图片描述
在这里插入图片描述

Gson

简介

Gson是Google官方解析JSON的工具,使用该工具需要导入该工具的JAR包到lib文件夹中。

Gson下载地址

在这里插入图片描述
在这里插入图片描述

构造方法
public Gson()       :       创建一个Gson对象,用来解析Json
常用方法
public String  toJson(Object o)     :       将Java对象转换为JSON格式的字符串

public T fromJson(String json, T.class)     :       将JSON格式的字符串转换为Java对象

public HashMap fromJson(String json, HashMap.class)     :     将JSON格式的字符串转换为HashMap,如果value中包含了数组["a","b","c"],那么数组将转为ArrayList
demo : 将Java对象转换为JSON格式的字符串
public class Book {
    String id;
    String name;
    String info;

    public Book(){}
    public Book(String id, String name, String info){
        this.id = id;
        this.name = name;
        this.info = info;
    }
}
import com.google.gson.Gson;

public class Demo01 {
    public static void main(String[] args) {
        Book b = new Book("111", "金苹果", "种植苹果");
        String json = new Gson().toJson(b);
        System.out.println(json); 	// {"id":"111","name":"金苹果","info":"种植苹果"}
    }
}

demo : 将JSON格式的字符串转换为Java对象
import com.google.gson.Gson;

public class Demo01 {
    public static void main(String[] args) {
        String json = "{\"id\":\"111\",\"name\":\"金苹果\",\"info\":\"种植苹果\"}";
        Book b1 = new Gson().fromJson(json, Book.class);
        System.out.println(b1.id);  	// 111
    }
}
demo : 将JSON字符串转换为Map,数组将转换为ArrayList
import com.google.gson.Gson;

import java.util.HashMap;

public class Demo01 {
    public static void main(String[] args) {
        String json3 = "{\"id\":\"1212\",\"name\":\"银苹果\",\"info\":[\"a\",\"b\",\"c\",\"d\",\"e\",\"f\",\"g\",\"h\"]}";
        HashMap data = new Gson().fromJson(json3, HashMap.class);
        System.out.println(data.get("info"));       // [a, b, c, d, e, f, g, h]
        System.out.println(data.get("info").getClass());        // class java.util.ArrayList
    }
}

FastJson

简介

FastJson是一个阿里集团开源的库,用来快速转换JSON文件。

使用FastJson需要导入JAR包到lib文件夹中。

FastJson下载地址

在这里插入图片描述

常用方法
(JSON) public static toJSONString(Object o)    :       将Java对象转换为JSON格式的字符串

(JSON) public static T parseObject(String json, T.class)    :   将JSON格式的字符串转换为Java对象

(JSON) public static List<T> parseArray(String json, T.class)   :   将数组转换为List
demo : Java对象转换为JSON格式的字符串
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;
    }

    public void setName(String name) {
        this.name = name;
    }
    public void setId(String id) {
        this.id = id;
    }
    public void setInfo(String info) {
        this.info = info;
    }

    public String getName(){
        return this.name;
    }
    public String getId(){
        return this.id;
    }
    public String getInfo(){
        return this.info;
    }
}
import com.alibaba.fastjson.JSON;

public class Demo02 {
    public static void main(String[] args) {
        Book b = new Book("1212", "黑苹果", "种苹果好辛苦");
        String json = JSON.toJSONString(b);     // 使用FastJson,需要实现Getter和Setter
        System.out.println(json);       // {"id":"1212","info":"种苹果好辛苦","name":"黑苹果"}
    }
}
demo: 将JSON格式的字符串转换为Java对象
public class Demo {
    public static void main(String[] args) {
        String json = "{\"id\":100\",\"name\":\"金苹果\", \"info\":\"种植苹果的真辛苦\",\"page\":\"[\"锄禾日当午\", \"汗滴禾下土\", \"嘿嘿嘿嘿嘿\"]}";
        Book b = JSON.parseObject(json, Book.class);
        System.out.println(b.getId());  // 100
    }
}
demo: 数组转换为ArrayList
import com.alibaba.fastjson.JSON;

import java.util.List;

public class Demo02 {
    public static void main(String[] args) {
        String json = "[\"a\",\"b\",\"c\",\"d\"]";
        List<String> list = JSON.parseArray(json, String.class);
        System.out.println(list.size());    // 4
        System.out.println(list);      // [a, b, c, d]
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值