2017-10-07 56 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(); }