[英]Parsing nested json array in java
I have json file in below format. 我有以下格式的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
}
Here "data" is array of projects, and within project attribute there is array of "issue". 这里的“数据”是项目的数组,在项目属性中是“问题”的数组。
So far if I remove "issue" array, I am able to traverse the json to one level down in "data" attribute, If this json has "issue" array I get an error saying missing comma. 到目前为止,如果我删除“问题”数组,则可以在“数据”属性中向下遍历json,如果此json具有“问题”数组,我会收到一条错误消息,说缺少逗号。
javax.json.stream.JsonParsingException: Invalid token=NUMBER at (line no=15, column no=14, offset=242) Expected tokens are: [COMMA]
Below is the code that I have right now. 下面是我现在拥有的代码。 I have two problems with this, one is the error while reading if I place the "issue" attribute, and secondly a way to read the "issue" array and traverse all attributes within. 我对此有两个问题,一个是在放置“ 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"));
}
*/
}
Any help or pointers is deeply appreciated. 任何帮助或指示深表感谢。 I can tweak the json if its required ultimately I need to maintain an array of issues. 如果最终需要,我可以调整json,我需要维护一系列问题。
The problem as others said is the JSON. 正如其他人所说,问题是JSON。 "id": 00001 <-- this is a number, numbers cannot start with a leading zero as per JSON stadard. “ id”:00001 <-这是一个数字,根据JSON标准,数字不能以前导零开头。 If you control the JSON you should tweak it. 如果您控制JSON,则应对其进行调整。
Alternatively ff you don't, you can use a less strict parser like org.json.simple
https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple The code will be the same as yours, just adjusted to org.json.simple
或者,如果您不这样做,则可以使用不太严格的解析器,例如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();
}
Your json data is invalid.You can check here. 您的json数据无效。您可以在此处检查。 http://jsonlint.com http://jsonlint.com
...issue": [{ "id": 00001,
"status": ----------------------^...issue": [{ "id": 00001,
Your id must be string number,string,boolean.Send 1,2,3,.... as return values and check if it works.
"status": ----------------------^...issue": [{ "id": 00001,
您的ID必须是字符串数字,字符串,布尔值。发送1,2,3,....为返回值并检查它是否有效。
"status": ----------------------^
Your code looks okay the problem is the JSON formatting. 您的代码看起来不错,问题在于JSON格式。 Specifically the following lines: 具体来说,以下几行:
"id": 00001, "id": 00002, "id": 00003, "id": 00004, “ id”:00001,“ id”:00002,“ id”:00003,“ id”:00004,
Basically if you want it in that format you will need to set them as strings by wrapping the values in quotations ie "id": "00001" or you can use a valid number ie "id": 1 基本上,如果您希望使用这种格式,则需要通过将值用引号引起来将它们设置为字符串,即“ id”:“ 00001”,或者可以使用有效数字即“ id”:1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.