아래 형식의 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"배열을 제거하면 "data"속성에서 json을 한 수준 아래로 이동할 수 있습니다.이 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 표준에 따라 선행 0으로 시작할 수 없습니다. 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();
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다