2015-02-24 89 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