这是我的JSON。告诉我一种使用JSON.org解析它的方法。我能够解析到photpath。如何继续进行,我想检查“Hascomment”字段,然后继续“注释”数组。怎么做?
{
"success":1,
"complaints":[
{
"complaintID":"1",
"userName":"Ganesh ",
"Title":"Check HelloMLA ",
"Description":"This is to check HelloMLA app after finishing image upload and download ",
"DOS":"02\/18\/15 21:14",
"Ago":"5 days ago",
"Status":"SOLVED",
"Photopath":"MLA\/images\/complaints\/1\/IMG_20141117_125245.jpg",
"Hascomment":"3",
"comments":[]
},
{
"complaintID":"2",
"userName":"Ganesh ",
"Title":"Check HelloMLA 2",
"Description":"This is to check HelloMLA app after finishing image upload and download 2",
"DOS":"02\/18\/15 21:14",
"Ago":"5 days ago",
"Status":"ANSWERED",
"Photopath":"MLA\/images\/complaints\/2\/IMG_20140806_120618.jpg",
"Hascomment":"1",
"comments":[]
},
{
"complaintID":"3",
"userName":"Ganesh ",
"Title":"Check HelloMLA 3",
"Description":"This is to check HelloMLA app after finishing image upload and download 3",
"DOS":"02\/18\/15 21:16",
"Ago":"5 days ago",
"Status":"UNSOLVED",
"Photopath":"MLA\/images\/complaints\/3\/IMG_20140808_121345.jpg",
"Hascomment":"0"
},
{
"complaintID":"4",
"userName":"Ganesh ",
"Title":"Check HelloMLA 4",
"Description":"This is to check HelloMLA app after finishing image upload and download ",
"DOS":"02\/18\/15 21:16",
"Ago":"5 days ago",
"Status":"UNSOLVED",
"Photopath":"MLA\/images\/complaints\/4\/IMG_20140820_175353.jpg",
"Hascomment":"0"
},
{
"complaintID":"28",
"userName":"Ganesh ",
"Title":"hello Gurudaths",
"Description":"Gurudaths93 is default email",
"DOS":"02\/21\/15 12:29",
"Ago":"3 days ago",
"Status":"UNSOLVED",
"Photopath":"MLA\/images\/complaints\/28\/IMG-20141205-WA0010.jpg",
"Hascomment":"0"
},
{
"complaintID":"35",
"userName":"Ganesh ",
"Title":"hello how r u",
"Description":"Naanu fine Neenu",
"DOS":"02\/23\/15 5:41",
"Ago":"1 day ago",
"Status":"UNSOLVED",
"Photopath":"MLA\/images\/complaints\/35\/Brain-erasure-300x225.jpg",
"Hascomment":"0"
},
{
"complaintID":"36",
"userName":"Ganesh ",
"Title":"fhj ajji ",
"Description":"hjhvcf chjj chhbji ",
"DOS":"02\/23\/15 5:42",
"Ago":"1 day ago",
"Status":"UNSOLVED",
"Photopath":null,
"Hascomment":"0"
}
]
}
答案 0 :(得分:0)
所以投诉键有一个对象数组。每个对象都有例如 complaintID , userName ,标题等。您可以在JSONArray中获取投诉的价值。然后循环遍历 JSONArray 并使用JSONObject在length()中获取每个对象。然后,您可以从 JSONObject 访问所有项目。 JSONObject 类有一个方法has(String name),您可以在获取其值之前检查项目是否存在于 JSONObject 中。
伪码:
if(jsonObject.has("success")) {
try {
Log.d("TAG", String.valueOf(jsonObject.getInt("success")));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(jsonObject.has("complaints")) {
try {
JSONArray jsonArray = jsonObject.getJSONArray("complaints");
for (int i = 0; i < jsonArray.length(); ++i) {
JSONObject jsonChildObject = jsonArray.getJSONObject(i);
if(jsonChildObject.has("complaintID")) {
Log.d("TAG", jsonChildObject.getString("complaintID"));
}
if(jsonChildObject.has("userName")) {
Log.d("TAG", jsonChildObject.getString("userName"));
}
// .
// .
// .
// .
// .
// .
if(jsonChildObject.has("Photopath") && jsonChildObject.getString("Photopath") != null) {
Log.d("TAG", jsonChildObject.getString("Photopath"));
}
if(jsonChildObject.has("Hascomment")) {
Log.d("TAG", jsonChildObject.getString("Hascomment"));
}
if(jsonChildObject.has("comments")) {
JSONArray jsonCommentArray = jsonChildObject.getJSONArray("comments");
for (int j = 0; j < jsonCommentArray.length(); ++j) {
// Print comments using index as j.
}
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}