2017-10-07 63 views
1

這是一個JSON,我從colorArray得到了結果(值),但是我無法從shapeArray獲得結果。如何從shapeArray獲取結果?如何獲得shapeArray值?如何管理這些類型的嵌套JSON響應?如何在android中訪問嵌套的JSON響應?

[ 
    { 

      "colorArray":[ 
        { 
        "colorName":"red", 
        "hexValue":"#f00" 
       }, 
        { 
        "colorName":"green", 
        "hexValue":"#0f0" 
       }, 
        { 
        "colorName":"blue", 
        "hexValue":"#00f" 
       }, 
        { 
        "colorName":"cyan", 
        "hexValue":"#0ff" 
       }, 
        { 
        "colorName":"magenta", 
        "hexValue":"#f0f" 
       }, 
        { 
        "colorName":"yellow", 
        "hexValue":"#ff0" 
       } 
      ] 
     }, 
     { 
      "shapeArray":[ 
       { 
        "shapeName":"circle" 
       }, 
       { 
        "shapeName":"square" 
       }, 
       { 
        "shapeName":"triangle" 
       }, 
       { 
        "shapeName":"hexagon" 
       } 
      ] 
     } 
    ] 

代碼

for (int i = 0; i < jsonArray.length(); i++) 
{ jsonObject = jsonArray.getJSONObject(i); 
JSONArray jsonColorArray = jsonObject.getJSONArray("colorArray"); 
for (int j=0;j<jsonColorArray.length();j++) 
    { JSONObject colorObj = jsonColorArray.getJSONObject(j); 
    String colorName = colorObj.getString("colorName"); 
    String hexValue = colorObj.getString("hexValue"); 
    } 
} 
+0

我的代碼:對(INT I = 0; I suryac

+0

你面臨什麼問題? –

+0

我無法獲取形狀數組值。如何從Json數組獲取shapeArray值? – suryac

回答

1

首先非常糟糕的json設計。秒

JSONArray jsonShapeArray = jsonObject.getJSONArray(「shapeArray」);

會給你錯誤,因爲沒有這樣的對象。試試這個:

JSONArray jsonArray = parent.getJSONObject(1).getJSONArray(「shapeArray」);

這裏的父母是你的根jsonArray。

+0

謝謝。它解決了我的問題。謝謝adnaan.zohran – suryac

0
JSONArray jsonColorArray = jsonArray.getJSONObject(0).getJSONArray("colorArray"); 
    JSONArray jsonShapeArray = jsonArray.getJSONObject(1).getJSONArray("shapeArray"); 
for (int j=0;j<jsonColorArray.length();j++) 
{ 
    JSONObject colorObj = jsonColorArray.getJSONObject(j); 
    String colorName = colorObj.getString("colorName"); 
    String hexValue = colorObj.getString("hexValue"); 
} 
for (int k=0;k<jsonShapeArray.length();k++) 
{ 
    JSONObject shapeObj = jsonShapeArray.getJSONObject(k); 
    String shapeName = shapeObj.getString("shapeName"); 

} 

希望這有助於你...如果您需要任何幫助,您可以問

+0

謝謝,但JSONArray jsonShapeArray = jsonObject.getJSONArray(「shapeArray」);給出例外 – suryac

0

jsonObject.getJSONArray()總是給人一種NullPointerException因爲兩個JSONArray名字是 不同。

既然你遍歷JSONArray可以處理該異常 這樣

 try { 
      JSONArray jsonArray = new JSONArray(jsonText); 
      for (int i = 0; i < jsonArray.length(); i++){ 
       JSONObject jsonObject = (JSONObject) jsonArray.get(i); 
       try { 
        JSONArray jsonColorArray = jsonObject.getJSONArray("colorArray"); 
        for (int j = 0; j < jsonColorArray.length(); j++) { 
         JSONObject jsonColorObject = (JSONObject) jsonColorArray.get(j); 
         String colorName = jsonColorObject.getString("colorName"); 
         String hexValue = jsonColorObject.getString("hexValue"); 
        } 
       } catch (JSONException ex) { 

       } 

       try { 
        JSONArray jsonShapeArray = jsonObject.getJSONArray("shapeArray"); 
        for (int k = 0; k < jsonShapeArray.length(); k++) { 
         JSONObject jsonShapeObject = (JSONObject) jsonShapeArray.get(k); 
         String shapeName = jsonShapeObject.getString("shapeName"); 
        } 
       } catch (JSONException ex) { 

       } 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

希望它會幫助你。

+0

我修改了你的代碼,只改動了一點點。謝謝 – suryac

0
try { 
       if (jsonResult != null) { 
        JSONArray jsonArray = null; 
        JSONObject jsonObject = null; 
        try { 
         jsonArray = new JSONArray(jsonResult); 

         for (int i = 0; i < jsonArray.length(); i++) { 
          jsonObject = jsonArray.getJSONObject(i); 
          JSONArray jsonColorArray = jsonObject.getJSONArray("colorArray"); 
          for (int j=0;j<jsonColorArray.length();j++){ 

           JSONObject colorObj = jsonColorArray.getJSONObject(j); 
           String colorName = colorObj.getString("colorName"); 
           String hexValue = colorObj.getString("hexValue"); 
          } 

          JSONArray shapeArray = jsonArray.getJSONObject(1).getJSONArray("shapeArray"); 
          JSONObject shapeObject = jsonObject.getJSONArray("shapes").getJSONObject(i); 

          for (int k = 0; k < shapeArray.length(); k++) { 
           JSONObject jsonShapeObject = (JSONObject) shapeArray.get(k); 
           String shapeName = jsonShapeObject.getString("shapeName"); 
          } 
         } 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
      } catch (Exception e) { 
       e.printStackTrace(); }