从json数组中过滤所需的数据

时间:2015-04-09 11:23:32

标签: android json

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

这是应该发生的事情, 当sizeSelectedItem = Sausage Crust Large然后resultTopping应该是两个Add BBQ Chicken-120.00和一个Add Cheese-120.00时,它们与Sausage Crust Large相关。

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

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",
   }

1 个答案:

答案 0 :(得分:1)

根据您的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;
}