访问2个数组内的数据

时间:2015-02-26 09:16:12

标签: android arrays json

我正在使用2个数组内部的尝试加载数据。目前我已在数组中成功加载了items数组。当我尝试访问最后一个数组中的数据时,它不会加载。

我有来自json response的这个数组

{  
      "Category":"1PI",
      "Description":"Chicken Bacon",
            "MainCategoryID":1,
      "MenuDescription":"Menu Description ",
      "SubMenuEntity":[  
         {  
            "Crust":"Sausage",
            "Description":"Sausage Crust Large",
            "ExtraItemEntity":[  
               {  
                  "Description":"Add BBQ Chicken-120.00",

第二个数组以SubMenuEntity开头,我加载了它。我想加载数组ExtraItemEntity中的数据描述。我怎样才能做到这一点? 这是数组内部数组内部数据的android代码。

    @Override
    public void onTaskCompleted(JSONArray responseJson) {

        try {
            List<String> crust = new ArrayList<String>();
            List<String> description = new ArrayList<String>();
            List<String> extraDescription = 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")) {

                    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"));

                        Log.i("Description", subMenuObject.getString("Description"));
                        description.add(subMenuObject.getString("Description"));
                    }

//                  JSONArray ExtraItemEntity = object.getJSONArray("ExtraItemEntity");
//                  for (int j = 0; j < ExtraItemEntity.length(); ++j) {
//                      JSONObject extraItemEntityObject = ExtraItemEntity
//                              .getJSONObject(j);
//                      Log.i("Description", extraItemEntityObject.getString("Description"));
//                      extraDescription.add(extraItemEntityObject.getString("Description"));
//                  }

                }
                crustSP = (Spinner) findViewById(R.id.sp_crust);
                ArrayAdapter<String> dataAdapterCru = new ArrayAdapter<String>(this,
                        android.R.layout.simple_spinner_item, crust);
                dataAdapterCru
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                crustSP.setAdapter(dataAdapterCru);

                sizeSP = (Spinner) findViewById(R.id.sp_pizza_size);
                ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(this,
                        android.R.layout.simple_spinner_item, description);
                dataAdapterDes
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                sizeSP.setAdapter(dataAdapterDes);

//              extraDescriptionOneSP = (Spinner) findViewById(R.id.sp_extratoppingone);
//              ArrayAdapter<String> dataAdapterExtraDesOne = new ArrayAdapter<String>(this,
//                      android.R.layout.simple_spinner_item, extraDescription);
//              dataAdapterExtraDesOne
//              .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//              extraDescriptionOneSP.setAdapter(dataAdapterExtraDesOne);
//              
//              extraDescriptionTwoSP = (Spinner) findViewById(R.id.sp_extratoppingtwo);
//              ArrayAdapter<String> dataAdapterExtraDesTwo = new ArrayAdapter<String>(this,
//                      android.R.layout.simple_spinner_item, extraDescription);
//              dataAdapterExtraDesTwo
//              .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//              extraDescriptionTwoSP.setAdapter(dataAdapterExtraDesTwo);

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

更新了代码

@Override
public void onTaskCompleted(JSONArray responseJson) {

    try {
        List<String> crust = new ArrayList<String>();
        List<String> description = new ArrayList<String>();
        List<String> extraDescription = 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")) {

                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"));

                    Log.i("Description", subMenuObject.getString("Description"));
                    description.add(subMenuObject.getString("Description"));


                    extraDescription.add(subMenuObject.getJSONArray("ExtraItemEntity").getJSONObject(j).getString("Description"));{

                    }
                }

            }
            crustSP = (Spinner) findViewById(R.id.sp_crust);
            ArrayAdapter<String> dataAdapterCru = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, crust);
            dataAdapterCru
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            crustSP.setAdapter(dataAdapterCru);

            sizeSP = (Spinner) findViewById(R.id.sp_pizza_size);
            ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, description);
            dataAdapterDes
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            sizeSP.setAdapter(dataAdapterDes);

          extraDescriptionOneSP = (Spinner) findViewById(R.id.sp_extratoppingone);
          ArrayAdapter<String> dataAdapterExtraDesOne = new ArrayAdapter<String>(this,
                  android.R.layout.simple_spinner_item, extraDescription);
          dataAdapterExtraDesOne
          .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          extraDescriptionOneSP.setAdapter(dataAdapterExtraDesOne);

          extraDescriptionTwoSP = (Spinner) findViewById(R.id.sp_extratoppingtwo);
          ArrayAdapter<String> dataAdapterExtraDesTwo = new ArrayAdapter<String>(this,
                  android.R.layout.simple_spinner_item, extraDescription);
          dataAdapterExtraDesTwo
          .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          extraDescriptionTwoSP.setAdapter(dataAdapterExtraDesTwo);

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

enter image description here

4 个答案:

答案 0 :(得分:1)

你做错了什么

描述实际上是一个json对象,因此应该使用json对象访问。

从那个json对象中获取String。 用此替换突出显示的部分

    JSONArray  extraItemEntityArray = subMenuobject.getJsonArray("ExtraItemEntity");

             for(int i=0;i< extraItemEntityArray.length();i++){
                 String extraItemdescription=extraItemEntityArray.getJSONObject(i).getString("Description");

                description.add(extraItemdescription);

Log.i("Description from array", extraItemdescription);   
             }       
Log.i("Description",subMenuObject.getString("Description"));        

如何正确行事

//获取包含对象的数组

JSONArray extraItemEntity = object.getJSONArray("ExtraItemEntity");

//从上面的数组中获取一个特定的对象。

JSONObject extraItemObject = extraItemEntity
                            .getJSONObject(i);

//现在从所选对象中检索描述为字符串(上图)。

String description = extraItemObject.getString("Description");

答案 1 :(得分:0)

ExtraItemEntity数组位于SubMenuEntity数组中。

所以,你可以在下面找到它。

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"));

                    Log.i("Description", subMenuObject.getString("Description"));
                    description.add(subMenuObject.getString("Description"));

                    JSONArray subExtraItemEntity = object.getJSONArray("ExtraItemEntity");
                    for (int k = 0; j < subExtraItemEntity.length(); ++j) {
                    JSONObject subExtraItemObject = subExtraItemEntity
                            .getJSONObject(k);

                    Log.i("ExtraItemEntity Description", subExtraItemObject.getString("Description"));
                    description.add(subExtraItemObject.getString("Description"));
                } 
    }

答案 2 :(得分:0)

JSONObject object = responseJson.getJSONObject(i);
JSONArray extraItemEntity = object.getJSONArray("ExtraItemEntity");
JSONObject extraItemObject = extraItemEntity.getJSONObject(i);
String description = extraItemObject.getString("Description");

答案 3 :(得分:0)

You can do this :
 for (int i = 0; i < responseJson.length(); ++i) {
    JSONObject object = responseJson.getJSONObject(i);

    if ((object.getString("MainCategoryID")).equals("1")
            && (object.getString("SubCategoryID")).equals("1")) {

        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"));

            subMenuObject.getString("Description"));
            description.add(subMenuObject.getString("Description"));

         if(subMenuObject.get("ExtraItemEntity") instanceof JSONArray){
             JSONArray  jsonArray=subMenuobject.getJsonArray("ExtraItemEntity");

         for(int i=0;i<jsonArray.length();i++){
             String extraItemdescription=jsonArray.getJSONObject(i).getString("Description");
         }

         }

        }

    }