java - 在java中解析嵌套的json数组

标签 java arrays json

我有以下格式的 json 文件。

{
    "data":[ 
        {
          "prjId": 1,
          "name" : "Forj1",
          "issue": [
                    {
                      "id": 00001,
                      "status" : "Closed"
                    },
                    {
                      "id": 00002,
                      "status" : "Open"
                    }
                ]
          },  
          {
          "prjId": 2,
          "name" : "Forj2",
          "issue": [
                    {
                      "id": 00003,
                      "status" : "Closed"
                    },
                    {
                      "id": 00004,
                      "status" : "Open"
                    }
                ]
          }],
    "issueCounter": 7,
    "success": true
}

这里的“数据”是项目数组,在项目属性中有“问题”数组。

到目前为止,如果我删除“issue”数组,我就能够将 json 遍历到“data”属性中的下一级,如果此 json 具有“issue”数组,我会收到一条错误消息,指出缺少逗号。

javax.json.stream.JsonParsingException: Invalid token=NUMBER at (line no=15, column no=14, offset=242) Expected tokens are: [COMMA]

下面是我现在拥有的代码。我对此有两个问题,一个是如果我放置“issue”属性,则读取时会出错,第二个是读取“issue”数组并遍历其中所有属性的方法。

InputStream fis = new FileInputStream(pathToFile+"data3.json");
JsonReader jsonReader = Json.createReader(fis);

//the error is thrown on below line while reading the above json. 

JsonObject jsonObject = jsonReader.readObject();

jsonReader.close();
fis.close();

System.out.println(jsonObject.getInt("issueCounter"));

//reading arrays from json
JsonArray jsonArrayData = jsonObject.getJsonArray("data");
Project [] prj = new Project[jsonArrayData.size()];
int index = 0;
for(JsonValue value : jsonArrayData){

    JSONObject jsonObj = new JSONObject(value.toString());
    System.out.println(jsonObj.getString("name"));
    System.out.println(jsonObj.getInt("prjId"));

    //this is also the place where I am stuck, I know I need to construct an array out of it by obtaining issue attribute. Below is very very wrong.
    /*
    JsonArray jsonArrayIssue = jsonObj.getJsonArray("issue");
    for(JsonValue issue : jsonArrayIssue){

        JSONObject jsonIssueObj = new JSONObject(issue.toString());
        System.out.println(jsonIssueObj.getString("status"));
        System.out.println(jsonIssueObj.getInt("id"));
    }
    */
}

非常感谢任何帮助或指示。如果最终我需要维护一系列问题,我可以调整 json。

最佳答案

正如其他人所说,问题在于 JSON。 "id": 00001 <-- 这是一个数字,根据 JSON 标准,数字不能以前导零开头。 如果你控制 JSON,你应该调整它。

或者,如果您不这样做,您可以使用不太严格的解析器,例如 org.json.simple https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple 代码将与您的相同,只是调整为 org.json.simple

try {   ...
        JSONObject rootJSON = (JSONObject) new JSONParser().parse(jsonString);
        JSONArray dataList = (JSONArray) rootJSON.get("data");
        for(Object projectObj: dataList.toArray()){
            JSONObject project = (JSONObject)projectObj;
            JSONArray issueList = (JSONArray) project.get("issue");
            for(Object issueObj: issueList.toArray()){
                JSONObject issue = (JSONObject) issueObj;
                //do something with the issue
            }
        }
    } catch (ParseException e) {
        //do smth
        e.printStackTrace();
    }

关于java - 在java中解析嵌套的json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41016764/

相关文章:

Java RMI 与 JFrame - 从客户端获取文本到 GUI

arrays - 使用最小堆实现排序数组的 K 路合并

javascript - 将字符串值解析为对象

json - Wannachart 不返回 png 图形

javascript - 如果没有 Node.js 或运行服务器,是否可以使用纯 JavaScript 通过 HTML 应用程序创建/输出 JSON 文件到硬盘?

java - 只允许某些类调用Java中的一个方法

java - 在 java 中将字符串转换为日期时出现意外输出

java - 无法在 Java 二维数组中赋值 - ArrayIndexOutOfBoundsException

java - 算术表达式计算 6.135157E-6 而不是零

c# - 如何固定字节数组?