Google Places Parse(排球)

时间:2018-10-26 09:13:23

标签: android json parsing android-volley

我正在尝试解析此文件。 我是Maps API的新手。 我正在尝试将此集解析为recyclerview 我尝试使用有效的String Request进行解析,但是我需要json对象来存储数据。

    {
   "html_attributions" : [],
   "result" : {
      "address_components" : [
         {
            "long_name" : "Ahmedabad",
            "short_name" : "Ahmedabad",
            "types" : [ "locality", "political" ]
         },
         {
            "long_name" : "Suradhara Society",
            "short_name" : "Suradhara Society",
            "types" : [ "neighborhood", "political" ]
         },
         {
            "long_name" : "Khodiar Nagar",
            "short_name" : "Khodiar Nagar",
            "types" : [ "sublocality_level_1", "sublocality", "political" ]
         },
         {
            "long_name" : "Ahmedabad",
            "short_name" : "Ahmedabad",
            "types" : [ "administrative_area_level_2", "political" ]
         },
         {
            "long_name" : "Gujarat",
            "short_name" : "GJ",
            "types" : [ "administrative_area_level_1", "political" ]
         },
         {
            "long_name" : "India",
            "short_name" : "IN",
            "types" : [ "country", "political" ]
         },
         {
            "long_name" : "382350",
            "short_name" : "382350",
            "types" : [ "postal_code" ]
         }
      ],
      "adr_address" : "3rd Floor, Sardar Patel Mall, Nikol Gam Road, \u003cspan class=\"street-address\"\u003eBapunagar\u003c/span\u003e, \u003cspan class=\"extended-address\"\u003eSuradhara Society, Khodiar Nagar\u003c/span\u003e, \u003cspan class=\"locality\"\u003eAhmedabad\u003c/span\u003e, \u003cspan class=\"region\"\u003eGujarat\u003c/span\u003e \u003cspan class=\"postal-code\"\u003e382350\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eIndia\u003c/span\u003e",
      "formatted_address" : "3rd Floor, Sardar Patel Mall, Nikol Gam Road, Bapunagar, Suradhara Society, Khodiar Nagar, Ahmedabad, Gujarat 382350, India",
      "formatted_phone_number" : "085116 15040",
      "geometry" : {
         "location" : {
            "lat" : 23.0379687,
            "lng" : 72.6433222
         },
         "viewport" : {
            "northeast" : {
               "lat" : 23.0393176802915,
               "lng" : 72.64467118029151
            },
            "southwest" : {
               "lat" : 23.0366197197085,
               "lng" : 72.64197321970849
            }
         }
      },
      "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
      "id" : "7ddb8968d27481b07a700f6e34ac5442f528eaf2",
      "international_phone_number" : "+91 85116 15040",
      "name" : "Parsana Gym",
      "photos" : [
         {
            "height" : 2322,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/104290859169763692879/photos\"\u003eAlpesh Patel\u003c/a\u003e"
            ],
            "photo_reference" : "CmRaAAAAgWV9QS54sRCALTitzLBc7LowfL3gKMw_jbNaTSmeFM9GqqG6iu1hx4HJwj332LJQHpfk44xeAPIgYg0mexUJaoWd8mKIUt4oTUj0f-DvbmnrOG67Q5BwAh9eP6VjcPz3EhAHDkHpn52M8rURjiAncwzrGhTLyHQZ5qHXowonHaOnDvld63wrXg",
            "width" : 4128
         }
      ],
      "place_id" : "ChIJEbpNLyCHXjkRny4b5TptHsY",
      "plus_code" : {
         "compound_code" : "2JQV+58 Ahmedabad, Gujarat, India",
         "global_code" : "7JMJ2JQV+58"
      },
      "rating" : 4.4,
      "reference" : "ChIJEbpNLyCHXjkRny4b5TptHsY",
      "reviews" : [
         {
            "author_name" : "Sumit Patel",
            "author_url" : "https://www.google.com/maps/contrib/101190972481759472779/reviews",
            "language" : "en",
            "profile_photo_url" : "https://lh3.googleusercontent.com/-70hcgZha3k4/AAAAAAAAAAI/AAAAAAAAAAA/ABtNlbDZJQHYZ86bOPzJBQjG8VNA0_Sh6g/s128-c0x00000000-cc-rp-mo/photo.jpg",
            "rating" : 5,
            "relative_time_description" : "4 months ago",
            "text" : "gym is good and trainer are also good.\nbut some new machine are require for excersize.",
            "time" : 1527730807
         },
         {
            "author_name" : "Mahesh Bhayani",
            "author_url" : "https://www.google.com/maps/contrib/105571914899766322779/reviews",
            "language" : "en",
            "profile_photo_url" : "https://lh3.googleusercontent.com/-EKevbjOaZsw/AAAAAAAAAAI/AAAAAAAAA4o/k75Wr5thZvg/s128-c0x00000000-cc-rp-mo/photo.jpg",
            "rating" : 5,
            "relative_time_description" : "4 months ago",
            "text" : "Good atmosfiyar for parsana gym.  Good service provide good  trinar.",
            "time" : 1527820640
         },
         {
            "author_name" : "Nadiya Dipak",
            "author_url" : "https://www.google.com/maps/contrib/100514831030336220877/reviews",
            "language" : "en",
            "profile_photo_url" : "https://lh6.googleusercontent.com/-he9oXwcUqLI/AAAAAAAAAAI/AAAAAAAAAAA/ABtNlbBq3ow6IHVx5QNQktTbXQEDI2v2xw/s128-c0x00000000-cc-rp-mo/photo.jpg",
            "rating" : 5,
            "relative_time_description" : "4 months ago",
            "text" : "Parsana gym is very good sarvice provide . Nice team work",
            "time" : 1527738686
         }
      ],
      "scope" : "GOOGLE",
      "types" : [ "gym", "health", "point_of_interest", "establishment" ],
      "url" : "https://maps.google.com/?cid=14275967968530345631",
      "utc_offset" : 330,
      "vicinity" : "3rd Floor, Sardar Patel Mall, Nikol Gam Road, Bapunagar, Ahmedabad"
   },
   "status" : "OK"
}

我使用了volley来提供解析异常。

这是我的代码  我正在尝试使用下面的代码使用凌空来解析JSON格式。

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {

            for (int i = 0; i < response.length(); i++) {

                try {

                    JSONObject object = response.getJSONObject(i);
                    JSONObject result = object.getJSONObject("result");

                    JSONArray array = result.getJSONArray("address_components");
                    Log.d("result", "" + array.length());
                    for (int adress = 0; adress < array.length(); adress++) {

                        JSONObject object1 = array.getJSONObject(i);

                        Log.d("result", object1.getString("long_name"));
                        Log.d("result", object1.getString("short_name"));

                    }


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


            }
        }

        //Error lisnter for request
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("error", error.getMessage());
        }
    });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsonArrayRequest );

任何人都可以帮我解决这个问题吗?

这是例外。 注意-:org.json.JSONException:值{“ html_attributions”:[],“结果”:{“ address_components”:[{“ long_name”:“ Ahmedab​​ad”,“ short_name”:“ Ahmedab​​ad”,“ t .........} enter image description here

谢谢!

1 个答案:

答案 0 :(得分:1)

执行此操作:

您的响应以jsonObject'{'而不是jsonArray'['

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
         try {
            JSONObject jsonObject = response.getJSONObject("result");
            JSONArray jsonArray = jsonObject.getJSONArray("address_components");

            for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject object = jsonArray.getJSONObject(i);
                        Log.d("result", object.getString("long_name"));
                        Log.d("result", object.getString("short_name"));

                    }

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

        //Error lisnter for request
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("error", error.getMessage());
        }
    });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsonArrayRequest );