我正在尝试使用下面显示的方法过滤数据以从json数组过滤数据。

这是应该发生的
sizeSelectedItem = Sausage Crust Large时,resultTopping应该是两个Add BBQ Chicken-120.00和一个Add Cheese-120.00,它们与“香肠硬皮”有关。

sizeSelectedItem = Pan Large时,resultTopping应该是一个Add BBQ Chicken-120.00,与Pan Pan有关。

List<String> topDescription全部是5个Add BBQ Chicken-120.00

更新的代码

   try {
                if ((object.getString("ItemCode")).equals(itemCode)) {

                    JSONArray subMenuArray = object
                            .getJSONArray("SubMenuEntity");

                    for (int j = 0; j < subMenuArray.length(); j++) {
                        JSONObject subMenuObject = subMenuArray
                                .getJSONObject(j);

                        if ((subMenuObject.getString("Description"))
                                .equals(sizeSelectedItem)) {

                            JSONArray extraItemEntityArray = subMenuObject
                                    .getJSONArray("ExtraItemEntity");

                            for (int k = 0; k < extraItemEntityArray.length(); k++) {
                                JSONObject objectE = extraItemEntityArray
                                        .getJSONObject(k);

                                if ((objectE.getString("Description")) != null
                                        && (objectE.getString("Type"))
                                                .equals("E")) {

                                    topDescription.add(objectE
                                            .getString("Description"));

                                    topPrice.add(objectE.getString("Price"));
                                    Log.i("ExtraDescriptionPrice",
                                            objectE.getString("Price"));

                                    Log.i("ExtraMainMenuCode", objectE
                                            .getString("ExtraMainMenuCode"));
                                    topMainMenuCode.add(objectE
                                            .getString("ExtraMainMenuCode"));

                                    Log.i("ExtraSubMenuCode", objectE
                                            .getString("ExtraSubMenuCode"));
                                    topSubMenuCode.add(objectE
                                            .getString("ExtraSubMenuCode"));

                                    Log.i("Type", objectE.getString("Type"));
                                    topType.add(objectE.getString("Type"));
                                }

                            }
                        }
                        break;
                    }
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


但这返回null,任何人都可以帮助我解决这个问题。

json回应

    {
          "ItemCode":"BPM ",
          "SubMenuEntity":[
             {
            "Description":"Sausage Crust Large",
            "ExtraItemEntity":[
               {
                  "Description":"Add BBQ Chicken-120.00",
               },
               {
                  "Description":"Add BBQ Chicken-120.00",
               },
               {
                  "Description":"Add Cheese-120.00",
               }
            ],
            "Crust":"Sausage"
         },
         {
            "Description":"Pan Large",
            "ExtraItemEntity":[
               {
                  "Description":"Add BBQ Chicken-120.00",
               }
            ],
            "Crust":"Pan"
         },
         {
            "Description":"Pan Mediam",
            "ExtraItemEntity":[
               {
                  "Description":"Add BBQ Chicken-120.00",
               },
               {
                  "Description":"Add BBQ Chicken-120.00",
               }
            ],
            "Crust":"Pan"
         },
         {
            "Description":"Personal Pan",
            "ExtraItemEntity":[

            ],
            "Crust":"Pan"
         }
      ],
      "Category":"1PI",
   }

最佳答案

根据您的json响应,如果要获取与Description属性相关的浇头,则应尝试执行以下操作:

public ArrayList<String> getFilteredToppings(String sizeSelectedItem) {
    ArrayList<String> resultTopping = new ArrayList<String>();
    sizeSelectedItem = sizeSP.getSelectedItem().toString();
    JSONObject jobj=new JSONObject("json response");
    JSONArray SubMenuEntity= jobj.getJSONArray("SubMenuEntity");
    for(int k=0;k< SubMenuEntity.length();k++){
        JSONObject object=SubMenuEntity.getJSONObject(k);
        if(object.getString("Description").equals(sizeSelectedItem)){
            for(int j=0;j<object.getJSONArray("ExtraItemEntity").length();j++)
                resultTopping.add(object.getJSONArray("ExtraItemEntity").getJSONObject(j).getString("Description"));
            break;
        }
    }
    return resultTopping;
}

10-07 22:29
查看更多