JSON字符串解析为对象(Map/JSONObject、实体类、数组/JSONArray)

字符串解析

  • 一、字符串解析为Map/JSONObject/JSONArray对象
    • 1、字符串解析为Map
    • 2、字符串解析为JSONObject
    • 3、字符串解析为JSONArray
  • 二、字符串解析为实体类对象
    • 1、字符串解析为实体类对象(普通的)
    • 2、字符串解析为实体类对象(数组中的每个元素为实体类)
    • 3、字符串解析为实体类对象(实体类中嵌套实体类)

介绍:在项目中,有的时候需要对json串进行解析,解析为map、jsonObject这种对象,解析为自定义实体类这种对象,解析为数组或者JSONArray这种对象(其数组中的每个元素可能是一个map或者实体类对象),这里使用 fastjson 库中的 JSON 相关方法,下面有一些案例。

准备工作:需要引入 fastjson 依赖。


<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>fastjsonartifactId>
    <version>1.2.70version>
dependency> 

一、字符串解析为Map/JSONObject/JSONArray对象

1、字符串解析为Map

public class Test {
    public static void main(String[] args) {
        // 字符串-->map
        String str = "{" +
                "\"name\": \"awen\"," +
                "\"sex\": \"boy\"," +
                "\"age\": 18" +
                "}";
        Map map = JSON.parseObject(str, Map.class);
        System.out.println(map);
    }
}
// 返回结果:
// {sex=boy, name=awen, age=18}

2、字符串解析为JSONObject

public class Test {
    public static void main(String[] args) {
        // 字符串-->JSONObject
        String str = "{" +
                "\"name\": \"awen\"," +
                "\"sex\": \"boy\"," +
                "\"age\": 18" +
                "}";
        JSONObject jsonObject = JSONObject.parseObject(str);
        System.out.println(jsonObject);
    }
}
// 返回结果:
// {"sex":"boy","name":"awen","age":18}       

3、字符串解析为JSONArray

public class Test {
    public static void main(String[] args) {
        // 字符串-->JSONArray
        String str = "[{" +
                "\"name\": \"awen\"," +
                "\"sex\": \"boy\"," +
                "\"age\": 18" +
                "}]";
        JSONArray jsonArray = JSONArray.parseArray(str);
        System.out.println(jsonArray);
    }
}
// 返回结果:
// [{"sex":"boy","name":"awen","age":18}]

二、字符串解析为实体类对象

1、字符串解析为实体类对象(普通的)

public class Student {
    private String name;
    private String sex;
    private Integer age;
    // 省略getXX/setXX/toString
}
    
package com.awen;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class Test {
    public static void main(String[] args) {
        // 字符串-->实体类对象
        String str = "{" +
                    "\"name\": \"awen\"," +
                    "\"sex\": \"boy\"," +
                    "\"age\": 18" +
                    "}";
        Student student = JSON.parseObject(str, Student.class);
        System.out.println(student);
    }
}
// 返回结果:
// Student{name='awen', sex='boy', age=18}

2、字符串解析为实体类对象(数组中的每个元素为实体类)

public class Test {
    public static void main(String[] args) {
        // 字符串-->实体类数组
        String str2 = "[" +
                "{" +
                    "\"name\": \"awen\"," +
                    "\"sex\": \"boy\"," +
                    "\"age\": 18" +
                "},"+
                "{" +
                    "\"name\": \"zhangsan\"," +
                    "\"sex\": \"girl\"," +
                    "\"age\": 20" +
                "}"+
                "]";
        Student[] students = JSON.parseObject(str2, Student[].class);
        System.out.println(students[0]);
        System.out.println(students[1]);
        }
}
// 返回结果:
// Student{name='awen', sex='boy', age=18}

3、字符串解析为实体类对象(实体类中嵌套实体类)

public class City {
    private String name;
    private String code;
    private List<City> children;
    // 省略getXX/setXX/toString
}
    
public class Test {
    public static void main(String[] args) {
        // 字符串-->实体类嵌套
        String str3 = "[" +
                "{" +
                    "\"code\": \"beijin\"," +
                    "\"name\": \"北京\"," +
                    "\"children\": [" +
                        "{" +
                            "\"code\": \"haidianqu\"," +
                            "\"name\": \"海淀区\"," +
                            "\"children\": null" +
                        "}," +
                        "{" +
                        "\"code\": \"chaoyangqu\"," +
                        "\"name\": \"朝阳区\"," +
                        "\"children\": null" +
                        "}" +
                    "]" +
                "}" +
                "]";
        City[] city = JSON.parseObject(str3, City[].class);
        System.out.println(city[0]);
    }
}
// 返回结果:
// City{name='北京', code='beijin', children=[City{name='海淀区', code='haidianqu', children=null}, City{name='朝阳区', code='chaoyangqu', children=null}]}

你可能感兴趣的:(我的随笔,json,字符串)