繁体   English   中英

在Java中解析嵌套的json数组

[英]Parsing nested json array in java

我有以下格式的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
}

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

到目前为止,如果我删除“问题”数组,则可以在“数据”属性中向下遍历json,如果此json具有“问题”数组,我会收到一条错误消息,说缺少逗号。

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();
    }

您的json数据无效。您可以在此处检查。 http://jsonlint.com
...issue": [{ "id": 00001,
"status": ----------------------^
...issue": [{ "id": 00001,
"status": ----------------------^
您的ID必须是字符串数字,字符串,布尔值。发送1,2,3,....为返回值并检查它是否有效。

您的代码看起来不错,问题在于JSON格式。 具体来说,以下几行:

“ id”:00001,“ id”:00002,“ id”:00003,“ id”:00004,

基本上,如果您希望使用这种格式,则需要通过将值用引号引起来将它们设置为字符串,即“ id”:“ 00001”,或者可以使用有效数字即“ id”:1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM