Java解析JSON数据

时间:2024-03-02 15:29:20

使用第三方库com.alibaba.fastjson解析,依赖包

      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>fastjson</artifactId>
          <version>1.2.3</version>
      </dependency>

 

Json的类型

JSON 值可以是:

  • 数字(整数或浮点数)
  • 字符串(在双引号中)
  • 逻辑值(true 或 false)
  • 数组(在中括号中)
  • 对象(在大括号中)
  • null

Java解析Json对象

json模板数据

{
  "person": {
    "id": "123",
    "name": "name",
    "address": "address"
  }
}

使用

jsonObject.getJSONObject

使用第三方库,解析代码如下:

    @Test
    public void testJsonObject(){
        String jsonString = "{\n" +
                "  \"person\":{\n" +
                "    \"id\":\"123\",\n" +
                "    \"name\":\"name\",\n" +
                "    \"address\":\"address\"\n" +
                "  }\n" +
                "}";
        try {
            JSONObject jsonObject = JSONObject.parseObject(jsonString);
            JSONObject personObject = jsonObject.getJSONObject("person");
            if(personObject!=null){
                Person person = new Person();
                person.setId(personObject.getString("id"));
                person.setName(personObject.getString("name"));
                person.setAddress(personObject.getString("address"));
                System.out.println(person);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

运行结果如下:

Java解析Array数组

模板数据:

{
  "students": [
    "xiaoming",
    "xiaohua",
    "daming",
    "dahua"
  ]
}

使用

jsonObject.getJSONArray

使用第三方库,解析代码如下:

@Test
    public void testJsonArray(){
        String jsonString = "{\n" +
                "  \"students\": [\n" +
                "    \"xiaoming\",\n" +
                "    \"xiaohua\",\n" +
                "    \"daming\",\n" +
                "    \"dahua\"\n" +
                "  ]\n" +
                "}";
        try {
            JSONObject jsonObject = JSONObject.parseObject(jsonString);
            JSONArray jsonArray = jsonObject.getJSONArray("students");
            for (int i = 0; i <jsonArray.size(); i++) {
                System.out.println(jsonArray.get(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

运行结果如下:

Json解析ArrayObject数组对象

模板数据:

{
  "persons": [
      {
        "id": "1",
        "name": "xiaoming",
        "address": "address"
      },
      {
        "id": "2",
        "name": "xiaohua",
        "address": "address"
      },
      {
        "id": "3",
        "name": "daming",
        "address": "address"
      },
      {
        "id": "4",
        "name": "dahua",
        "address": "address"
      }
  ]
}

使用第三方库,解析代码如下:

@Test
    public void testJsonArrayAndObject(){
        String jsonString = "{\n" +
                "  \"persons\": [\n" +
                "    {\n" +
                "      \"id\": \"1\",\n" +
                "      \"name\": \"xiaoming\",\n" +
                "      \"address\": \"address\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": \"2\",\n" +
                "      \"name\": \"xiaohua\",\n" +
                "      \"address\": \"address\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": \"3\",\n" +
                "      \"name\": \"daming\",\n" +
                "      \"address\": \"address\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": \"4\",\n" +
                "      \"name\": \"dahua\",\n" +
                "      \"address\": \"address\"\n" +
                "    }\n" +
                "  ]\n" +
                "}";
        try {
            JSONObject jsonObject = JSONObject.parseObject(jsonString);
            JSONArray jsonArray = jsonObject.getJSONArray("persons");
            for (int i = 0; i <jsonArray.size(); i++) {
                JSONObject jsonArrayObjectItem = JSONObject.parseObject(jsonArray.get(i).toString());
                if(jsonArrayObjectItem!=null){
                    Person person = new Person();
                    person.setId(jsonArrayObjectItem.getString("id"));
                    person.setName(jsonArrayObjectItem.getString("name"));
                    person.setAddress(jsonArrayObjectItem.getString("address"));
                    System.out.println(person);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

解析结果如下:

 

 

下面来个小练习,可以下载这个地址的文件:简单的练习

链接:https://pan.baidu.com/s/1CWReej0FPmI1IHketA4Yxg 密码:5oy4

大家可以解析一下,练练手,这里就不公布答案了。以后有时间补上答案。我要求返回的数据格式是这样的。

{
  "sheet_num": "100",
  "result": [
    {
      "key?": "result",
      "NoNodeId": "?",
      "nodeId": "?"
    }
  ],
  "desc": "所有的{nodeId}相加",
  "Nodesc": "所有的{NoNodeId}相加",
  "text": "{desc} \n {Nodesc}"
}

key?即取所有value等于result的key,

取所有NoNodeId的值,并存储数组里面。

取所有nodeId的值,并存储数组里面。

desc 就是所有的nodeId相加

Nodesc 就是所有的NoNodeId相加

text,就是desc加上回车,再加Nodesc