2015-02-24 63 views
0

这里是我的JSON。告诉我一种解析这个使用JSON.org的方法。我能解析到photpath.How进一步进行,我想检查“Hascomment”字段,然后继续“评论”数组。如何做到这一点?解析可变长度的嵌套JSON

{ 
"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

你可以发布什么,直到你已经尽力了,这样我们就可以帮您 – 2015-02-24 16:01:24

回答

0

所以投诉键有对象的数组。每个物品都有物品complaintID的userName标题等,您可以在JSONArray得到投诉值。然后遍历JSONArray并使用length()获取JSONObject中的每个对象。然后您可以访问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(); 
      } 
     } 
+0

THKS一个lot..it工作对我来说.. – 2015-02-24 16:41:55