Volley JsonArrayRequest解析Json数据如何处理数组响应

时间:2016-03-31 10:26:44

标签: android arrays json android-volley

这里我想获得“类别”,并根据类别想要获得        “标题”& “价钱”      我已经从JsonSchema2Pojo

生成了pojo类

我使用Volley& amp处理响应GsonRequest类如:

     JsonArrayRequest req = new JsonArrayRequest(URL, new    Response.Listener<JSONArray>() {
             @Override
                  public void onResponse(JSONArray response) {
                     Log.d("response....",""+response);
                 if (response != null) {
                     showProgress(false);
                     Gson gson = new GsonBuilder().create();
                     try {
                         Log.d("response....", "G00D");

                     Item items = gson.fromJson(response,Item.class);

                     JSONArray json = new JSONArray(response);
                     Log.d("arrayJSon",""+json.get(0));

 //What is need to do here tell me solution please.........................   
 //.................dosomething here.......................   

                     }

我的json回应是:

[{
                  "category": "Espresso & Coffee",
                  "items": [{
                    "title": "Vacuum Coffee",
                    "size": [{
                        "label": "medium",
                        "price": 125
                    }, {
                    "label": "large",
                    "price": 145
                 }]
             }, {
                "title": "Cafe Latte",
                 "size": [{
                    "label": "small",
                    "price": 115
                }, {
                    "label": "medium",
                    "price": 135
                }, {
                    "label": "large",
                    "price": 150
                }]
            }, {
                "title": "Cafe Mocha",
                "size": [{
                    "label": "small",
                    "price": 115
                }, {
                    "label": "medium",
                    "price": 135
                }, {
                    "label": "large",
                    "price": 150
                }]
             }, {
                "title": "White Mocha",
                "size": [{
                    "label": "small",
                    "price": 120
                }, {
                    "label": "medium",
                    "price": 145
                 }, {
                    "label": "large",
                    "price": 155
                }]
            }, {
                "title": "Caramel Macchiato",
                "size": [{
                    "label": "small",
                    "price": 125
                }, {
                    "label": "medium",
                    "price": 145
                }, {
                    "label": "large",
                    "price": 165
                }]
            }, {
                "title": "Coffee De Leche",
                "size": [{
                    "label": "small",
                    "price": 130
                }, {
                    "label": "medium",
                    "price": 145
                }, {
                    "label": "large",
                    "price": 160
                }]
            }, {
                "title": "Cafe-UK Coffee Lava",
                "size": null,
                "price": 145
            }]
        }, {
            "category": "Frappe",
            "items": [{
                "title": "Coffee Base",
                "size": [{
                    "label": "small",
                    "price": 140
                }, {
                    "label": "medium",
                    "price": 155
                }, {
                    "label": "large",
                    "price": 170
                }]
            }, {
                "title": "Milk Base",
                "size": [{
                    "label": "small",
                    "price": 130
                }, {
                    "label": "medium",
                    "price": 145
                }, {
                    "label": "large",
                    "price": 160
                 }]
             }, {
                "title": "Cream Soda Float w\/Ice-cream",
                "size": null,
                "price": 140
            }]
        }, {
            "category": "Milk Tea & Juice",
            "items": [{
                "title": "Milk Tea",
                "size": [{
                    "label": "small",
                    "price": 105
                }, {
                    "label": "medium",
                    "price": 120
                }]
             }, {
                "title": "Hot Tea",
                "size": null,
                "price": 120
             }, {
                "title": "Italian Soda",
                "size": [{
                    "label": "small",
                    "price": 105
                }, {
                    "label": "medium",
                    "price": 120
                }, {
                    "label": "large",
                    "price": 140
                }]
            }, {
                "title": "Juice",
                "size": null,
                "price": 105
            }]
          }, {
            "category": "Food",
            "items": [{
                "title": "Ultimate Chili Con Fries",
                "size": null,
                "price": 290
            }, {
                "title": "Nachos",
                "size": null,
                "price": 290
             }, {
                "title": "Marble Potato Fondue",
                "size": null,
                "price": 120
             }, {
                 "title":"Breakfast",
                 "size":null,
                 "price":220
             }]}]

1 个答案:

答案 0 :(得分:3)

我遇到了与你前一段时间相同的情况,并最终手动处理json而不是gson。而不是使用JsonArrayRequest,尝试JsonObjectRequest。

之后,通过'砍掉'这样的回报得到每个数组:

JsonArray c = response.getJSONArray("array");

然后通过一个带有限制i<c.length().

的for循环来获得存储在数组中的值

但是,由于你有一个阵列的arrray,你将沿着这一行做点什么:

for(int i=0; i<c.length(); i++){
//populates the array, in your case, jsonarray size = 4
JSONObject jsonObject = c.getJSONObject(i);
String cat= jsonObject.getString("category"); //gets category String
JSONArray items = jsonObject.getJSONArray("items");
....

在这里,你编写了一个内部循环,以便在你的对象中获取数组项....我希望我帮助你解决问题,而不是让你困惑,哈哈。如果你需要一个例子,这里有一个帮我解决哪个问题:https://github.com/codepath/android_guides/wiki/Rotten-Tomatoes-Networking-Tutorial