在 json 响应中访问数组内部的数组

Accessing an array inside an array in json response

我正在使用以下代码获取价格值。我可以获得物有所值的价格。然而,当涉及到地壳时,它运行到异常。

@Override
    public void onTaskCompleted(JSONArray responseJson) {

        try {
            List<String> crust = new ArrayList<String>();   
            List<String> price = new ArrayList<String>();

            for (int i = 0; i < responseJson.length(); ++i) {
                JSONObject object = responseJson.getJSONObject(i);

                if ((object.getString("MainCategoryID")).equals("1")
                        && (object.getString("SubCategoryID")).equals("1")) {
                    Log.i("Price ", object.getString("Price"));
                    price.add(object.getString("Price"));
                    Log.i("Crust ", object.getString("Crust"));
                    crust.add(object.getString("Crust"));

                }

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

这是 json 响应

{  
      "Category":"1PI",
      "Price":0.0000,
      "SubCategoryID":1,
      "SubMenu":true,
      "SubMenuEntity":[  
         {  
            "Crust":"Sausage",

Crust 位于数组中的数组中,我如何在上面的代码中访问 c​​rust。

如有任何帮助,我们将不胜感激。

SubMenuEntity 项目有一个对象数组。您需要获取数组并循环遍历它。对于每个对象,获取 Crust.

的值
try {
      List<String> crust = new ArrayList<String>();
      List<String> price = new ArrayList<String>();

      JSONArray responseJson = null;

      for (int i = 0; i < responseJson.length(); ++i) {
        JSONObject object = responseJson.getJSONObject(i);

        if ((object.getString("MainCategoryID")).equals("1")
            && (object.getString("SubCategoryID")).equals("1")) {
          Log.i("Price ", object.getString("Price"));
          price.add(object.getString("Price"));

          JSONArray subMenuArray = object.getJSONArray("SubMenuEntity");
          for (int j = 0; j < subMenuArray.length(); ++j) {
            JSONObject subMenuObject = subMenuArray.getJSONObject(j);
            Log.i("Crust ", subMenuObject.getString("Crust"));
            crust.add(subMenuObject.getString("Crust"));
          }

        }

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

你的 CrustSubMenuEntity 里面,你应该使用 object.getJSONArray("SubMenuEntity") 得到这个,然后从这个数组 Object 的第一个

得到 Crust =]