如何在android中访问嵌套的JSON响应?

时间:2017-10-07 06:07:45

标签: android arrays json

这是一个JSON,我从colorArray得到了结果(值),但是我无法从shapeArray获得结果。如何从shapeArray获得结果?如何获得shapeArray值?如何管理这些类型的嵌套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"); 
   } 
} 

4 个答案:

答案 0 :(得分:1)

首先非常糟糕的json设计。第二个

  

JSONArray jsonShapeArray = jsonObject.getJSONArray(“shapeArray”);

由于没有这样的对象,

会给你错误。试试这个:

  

JSONArray jsonArray = parent.getJSONObject(1).getJSONArray(“shapeArray”);

这里父是你的根jsonArray。

答案 1 :(得分: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");

 } 

希望这有助于你...如果你需要任何帮助,你可以问

答案 2 :(得分: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();
        }

希望它会对你有所帮助。

答案 3 :(得分: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(); }