使用 simple-json 库解析 json 时出错

Error parsing json with simple-json library

我想得到这些Json的所有“name”,我尝试了iterator和mapString,但是没有成功,结果总是NULL.

try {
                    JSONParser parser = new JSONParser();

                    JSONObject json = (JSONObject) parser.parse(responseBody);
                    System.out.println("Parseamento: " + json);

                String projectName = (String) json.get("name");
                System.out.println("Count: " + projectName);


                } catch (Exception e) {
                    // TODO: handle exception
                }

这就是答案Json

{
   "count":02,
   "value":[
      {
         "visibility":"private",
         "name":"New Business",
         "description":"",
         "id":"",
         "state":"wellFormed",
         "url":"https:\/\/dev.azure.com\/work\/_apis\/projects\/ac9f8ec3-cb2c-4db9-b600-47e34b217e97",
         "revision":434,
         "lastUpdateTime":"2021-01-08T14:18:01.387Z"
      },
      {
         "visibility":"private",
         "name":"Doctor Strange",
         "id":"d0fa6bda-0699-4c33-a24f-c9dbe9655bdb",
         "state":"wellFormed",
         "url":"https:\/\/dev.azure.com\/work\/_apis\/projects\/d0fa6bda-0699-4c33-a24f-c9dbe9655bdb",
         "revision":330,
         "lastUpdateTime":"2020-10-01T17:57:23.58Z"
      }
   ]
}

com.googlecode.json-simple json-simple 1.1.1

试试这个

JSONObject json = (JSONObject)parser.parse(responseBody);
        JSONArray values = (JSONArray) json.get("value");
        for(Object o: values){
            if ( o instanceof JSONObject ) {
                String projectName = (String) ((JSONObject)o).get("name");
                System.out.println(projectName);
            }
        }

你的JSON中的name在数组values中。

  1. 获取该数组:
JSONArray projectArray = (JSONArray) json.get("value");
  1. 从数组的每个条目中获取 name:
Object[] projObjArray = projectArray.toArray();
for (int i=0; i<projObjArray.length;  i++) {
    JSONObject project = (JSONObject) projObjArray[i];
    String projectName = (String) project.get("name");
    System.out.println("Count: " + projectName);
}

输出:

Count: 2
Count: New Business
Count: Doctor Strange

完整代码:

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class Main {

    public static void main(String[] args) {
        String responseBody = "{\r\n" + "   \"count\":02,\r\n" + "   \"value\":[\r\n" + "      {\r\n"
                + "         \"visibility\":\"private\",\r\n" + "         \"name\":\"New Business\",\r\n"
                + "         \"description\":\"\",\r\n" + "         \"id\":\"\",\r\n"
                + "         \"state\":\"wellFormed\",\r\n"
                + "         \"url\":\"https:\/\/dev.azure.com\/work\/_apis\/projects\/ac9f8ec3-cb2c-4db9-b600-47e34b217e97\",\r\n"
                + "         \"revision\":434,\r\n" + "         \"lastUpdateTime\":\"2021-01-08T14:18:01.387Z\"\r\n"
                + "      },\r\n" + "      {\r\n" + "         \"visibility\":\"private\",\r\n"
                + "         \"name\":\"Doctor Strange\",\r\n"
                + "         \"id\":\"d0fa6bda-0699-4c33-a24f-c9dbe9655bdb\",\r\n"
                + "         \"state\":\"wellFormed\",\r\n"
                + "         \"url\":\"https:\/\/dev.azure.com\/work\/_apis\/projects\/d0fa6bda-0699-4c33-a24f-c9dbe9655bdb\",\r\n"
                + "         \"revision\":330,\r\n" + "         \"lastUpdateTime\":\"2020-10-01T17:57:23.58Z\"\r\n"
                + "      }\r\n" + "   ]\r\n" + "}";
        try {
            JSONParser parser = new JSONParser();

            JSONObject json = (JSONObject) parser.parse(responseBody);
            System.out.println("Parseamento: " + json);

            JSONArray projectArray = (JSONArray) json.get("value");
            System.out.println("Count: " + projectArray.toArray().length);

            Object[] projObjArray = projectArray.toArray();
            for (int i=0; i<projObjArray.length;  i++) {
                JSONObject project = (JSONObject) projObjArray[i];
                String projectName = (String) project.get("name");
                System.out.println("Count: " + projectName);
            }

        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}