如何从android中的嵌套json中捕获服务器的响应

时间:2018-03-09 13:31:25

标签: c# php android asp.net .net

我试图通过此链接在我的排球库中捕捉到响应

[
    {
        "data": {
            "1": {
                "del_id": "1",
                "image1": "",
                "image2": "",
                "image3": "",
                "moving_item": "gvxx",
                "pick_up": "Luton, UK",
                "drop_off": "Brasília - Brasilia, Federal District, Brazil",
                "lat_pickup": "51.8786707",
                "long_pickup": "-0.4200255000000001",
                "lat_dropoff": "-15.7942287",
                "long_dropoff": "-47.882165799999996",
                "distance": "5459",
                "add_type": "personal",
                "date": "23-02-2018",
                "totalbid": "0",
                "current_bid": null
            },
            "2": {
                "del_id": "2",
                "image1": "47cdb3b7deb1572db979bd7d5e940364.jpg",
                "image2": "23cd1ee8a710ac4b88625178538e3537.jpeg",
                "image3": "",
                "moving_item": "hshaj",
                "pick_up": "Lutz, FL, USA",
                "drop_off": "Bradenton, FL, USA",
                "lat_pickup": "28.151124299999996",
                "long_pickup": "-82.4614831",
                "lat_dropoff": "27.498927799999997",
                "long_dropoff": "-82.5748194",
                "distance": "45",
                "add_type": "personal",
                "date": "02-12-2018",
                "totalbid": "0",
                "current_bid": null
            }]

但它没有捕获并且通过异常我使用此代码来捕获我的android applciation中的响应。

 JSONObject obj = new JSONObject(response);
                            JSONArray c1 = obj.getJSONArray("data");
                            //JSONArray jsonArray = new JSONArray(response);
                            for (int i = 0; i < c1.length(); i++)
                            {
                                JSONObject searchlist = c1.getJSONObject(i);
                                searchmodel.add(new SearchModel(
                                        searchlist.optString("moving_item"),
                                        searchlist.optString("pick_up"),
                                        searchlist.optString("drop_off"),
                                        searchlist.optDouble("distance"),
                                        searchlist.optInt("totalbid"),
                                        searchlist.optInt("current_bid"),
                                        searchlist.optString("date"),
                                        searchlist.optString("image1")

                                ));
                            }

请帮我解决我的问题我正在努力解决这个问题我非常感谢如果有人带我离开这个谢谢你

1 个答案:

答案 0 :(得分:0)

嘿男人首先这不是获得这种类型逻辑的地方你必须搜索更多,直到你得到它但是认为它是你从android开始所以我提供你如何解析你的json验证答案如果它有权帮助别人我已经尝试过并且工作正常,希望这能帮到你:

try {

            JSONArray jsonRootArray = new JSONArray(json);
            for (int i=0;i<jsonRootArray.length();i++){
                JSONObject jsonObject = jsonRootArray.getJSONObject(i);
                JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                for (int j=1;j<=jsonObject1.length();j++){
                    JSONObject jsonObject2 = jsonObject1.getJSONObject(j+"");
                    String del_id = jsonObject2.getString("del_id");
                    Log.e("id",del_id);
                }
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

还有一个提示

开始之前只需检查http://jsonviewer.stack.hu/上的json,如果它无效,并且从中获知如何解析它。