Related
I am trying to parse this file.
I am new to maps API so.
I am trying to parse this set into recyclerview
I trying to parse using String Request that works but I need json object to store data.
{
"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"
}
I use volley that gives parse exception.
Here is my code
I am trying to parse JSON format using volley using below code.
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 );
Can Anyone help what I am missing in this?
here is exception.
Note-: org.json.JSONException: Value {"html_attributions":[],"result":{"address_components":[{"long_name":"Ahmedabad","short_name":"Ahmedabad","t.........}
Thanks in advance !!
Do this :
your response starts with jsonObject'{' not 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 );
I want to get data from this but its to confusing, how to do it.
I want to access legs. In legs distance then text.
Then after steps, in steps again distance and then text.
In "routes" ==> "legs" --> distance --> text
Then to get driving steps
"routes" ==> "legs" ==> "steps" --> distance --> text
First there is object then array again object its too confusing. Any help would be appreciated.
{
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "ChIJ2QeB5YMEGTkRYiR-zGy-OsI",
"types" : [ "locality", "political" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJ2afeeFcxOzkRL9RVTscv17o",
"types" : [ "locality", "political" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 31.55462439999999,
"lng" : 74.3571711
},
"southwest" : {
"lat" : 30.1981178,
"lng" : 71.4687352
}
},
"copyrights" : "Map data ©2016 Google",
"legs" : [
{
"distance" : {
"text" : "348 km",
"value" : 347978
},
"duration" : {
"text" : "4 hours 49 mins",
"value" : 17335
},
"end_address" : "Multan, Pakistan",
"end_location" : {
"lat" : 30.1981178,
"lng" : 71.4687352
},
"start_address" : "Lahore, Pakistan",
"start_location" : {
"lat" : 31.55462439999999,
"lng" : 74.3571711
},
"steps" : [
{
"distance" : {
"text" : "67 m",
"value" : 67
},
"duration" : {
"text" : "1 min",
"value" : 9
},
"end_location" : {
"lat" : 31.5549532,
"lng" : 74.3565735
},
"html_instructions" : "Head \u003cb\u003enorthwest\u003c/b\u003e on \u003cb\u003eAllama Iqbal Rd\u003c/b\u003e",
"polyline" : {
"points" : "k_r_Ei{ydM[r#e#bA"
},
"start_location" : {
"lat" : 31.55462439999999,
"lng" : 74.3571711
},
"travel_mode" : "DRIVING"
},
Manual method:
In few words Json gives you objects (beetwen { } ) or array (beetwen [ ] ). To get sth from deep in json you have to go through each level. You put only fragment of json, but I closed it and we have at start json object (because main braces are {} ):
{
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "ChIJ2QeB5YMEGTkRYiR-zGy-OsI",
"types" : [ "locality", "political" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJ2afeeFcxOzkRL9RVTscv17o",
"types" : [ "locality", "political" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 31.55462439999999,
"lng" : 74.3571711
},
"southwest" : {
"lat" : 30.1981178,
"lng" : 71.4687352
}
},
"copyrights" : "Map data ©2016 Google",
"legs" : [
{
"distance" : {
"text" : "348 km",
"value" : 347978
},
"duration" : {
"text" : "4 hours 49 mins",
"value" : 17335
},
"end_address" : "Multan, Pakistan",
"end_location" : {
"lat" : 30.1981178,
"lng" : 71.4687352
},
"start_address" : "Lahore, Pakistan",
"start_location" : {
"lat" : 31.55462439999999,
"lng" : 74.3571711
},
"steps" : [
{
"distance" : {
"text" : "67 m",
"value" : 67
},
"duration" : {
"text" : "1 min",
"value" : 9
},
"end_location" : {
"lat" : 31.5549532,
"lng" : 74.3565735
},
"html_instructions" : "Head \u003cb\u003enorthwest\u003c/b\u003e on \u003cb\u003eAllama Iqbal Rd\u003c/b\u003e",
"polyline" : {
"points" : "k_r_Ei{ydM[r#e#bA"
},
"start_location" : {
"lat" : 31.55462439999999,
"lng" : 74.3571711
},
"travel_mode" : "DRIVING"
}]
}
]
}
]
}
Let's say you have this as a String named "jsonString". You create json object like this:
JSONObject jsonObject = new JSONObject(jsonString);
Than you needs "routes" that is json array (between [] braces). You create this array going deeper into your main jsonObject:
JSONArray routesArray = jsonObject.getJSONArray("routes");
To go through each array element (that are json objects) you iterate like this:
for (int i = 0; i < routesArray.length(); i++) {
//do sth
}
And you get your "legs" like this:
JSONObject routeObject = routesArray.get(i);
routeObject.get("legs");
And deeper and deeper like above.
Automatically
It's much easier to use Gson library and some Json Pojo parsers.
You import gson in your build.gradle:
compile 'com.google.code.gson:gson:2.4'
Than you can use this parser. Select source type "JSON" and "Annotation style" Gson. Put your json in text box and click Preview or download ZIP. You can also put your package name and main class name or rename it manually later. You will be given multiple model classes with top one called "Example" or your name. It consists of two fields:
public class Example {
#SerializedName("geocoded_waypoints")
#Expose
private List<GeocodedWaypoint> geocodedWaypoints = new ArrayList<GeocodedWaypoint>();
#SerializedName("routes")
#Expose
private List<Route> routes = new ArrayList<Route>();
Add all classes to your project and than you just parse your jsonString to your main class like this:
Example mainClassObject = new Gson().fromJson(jsonString, Example.class);
And now you can access routes from this object. And legs from routes and deeper and deeper...
Please try below code to parse provided json keys value :
private void parseJson(String jsonString){
ArrayList<String> legsDistanceList = new ArrayList<>();
ArrayList<String> legStepsDistanceList = new ArrayList<>();
try{
JSONObject rootJson = new JSONObject(jsonString);
if(rootJson.has("routes")) {
JSONArray routesJsonArray = rootJson.getJSONArray("routes");
for(int i=0;i<routesJsonArray.length();i++){
JSONObject routeJson = routesJsonArray.getJSONObject(i);
if(routeJson.has("legs")){
JSONArray legsJsonArray = routeJson.getJSONArray("legs");
for(int j=0;j<legsJsonArray.length();j++){
JSONObject legJson = legsJsonArray.getJSONObject(j);
if(legJson.has("distance")){
JSONObject distanceJson = legJson.getJSONObject("distance");
legsDistanceList.add(distanceJson.getString("text"));
}
if(legJson.has("steps")){
JSONArray stepsJsonArray = legJson.getJSONArray("steps");
for(int k=0;k<stepsJsonArray.length();k++){
JSONObject stepJson = stepsJsonArray.getJSONObject(k);
if(stepJson.has("distance")){
JSONObject stepDistanceJson = stepJson.getJSONObject("distance");
legStepsDistanceList.add(stepDistanceJson.getString("text"));
}
}
}
}
}
}
}
}catch (Throwable e){
e.printStackTrace();
}
Log.i("Legs Distance ",legsDistanceList.toString());
Log.i("Legs Steps Distance ",legStepsDistanceList.toString());
}
I think above code might be difficult for you to understand at this stage but once you understand then it will more helpful to you for future JSON parsing stuff.
you can third parties(Gson, Logan Square,etc..) for parsing. If you are trying to access it directly. Below you can find the sample code to access the "legs" array.
Let us assume
JsonObject jsonObject = new JsonObject("json string");
this "jsonObject" is the full json value, from which you want to access the "legs" array. In this scenario the "legs" array is inside the "routes" array. So we need to get the "routes" array first and loop that array and get the "legs" array from each object and store in POJO class or if you want to fetch the "text" alone from the "distance" means you can store that in arraylist or string which you need. Below is the sample code to get the text alone.
JsonArray routesArray = jsonObject.optJsonArray("routes");
if (routesArray != null) {
for (int i = 0; i < routesArray.length(); i++) {
JsonArray legsArray = routesArray.get(i).optJsonArray("legs");
if (legsArray != null) {
for (int i = 0; i < legsArray.length(); i++) {
JsonObject legObject = legsArray.get(i);
JsonObject distanceObject = legObject.optJsonObject("distance");
String textValue = distanceObject.optString("text");
}
}
}
}
Hope this is helpful :)
I have a response from a google web service like the follwing
{
"html_attributions" : [],
"next_page_token" : "CpQCBgEAALxtLOBsfDviXsb8WHg4nGHrCETBydo0YcgOw-IHvFS4CQI1ZaM331dtA8Y3CxPeZZlF0IYjwQMp2A8W5A5UKtTrR4sQq1Um6FJgUNCpZzrcT6RwaPJKzOjbaFrPt5GqnQM6W1vxxdK9nKu5lyBbvLr0yJnzBWEAqyLyT2MFvak-_qDIR8b3yK3Efy34SsoHNnBnaANVc5hMztz7aWGphkTDNtEfuSZnQQ72jPg5_ey5F2G29in_QJXJlR9a3YYNGFmefLta2e0T34OGOhtvCinrdE7dcEUuaK55LV8TnP33HlGKC7PruXkv4AF8Xvxnlsk9ALVFdzECmJ4br6RTq3iWjBZ0z5FPNzqjfa6NNFiqEhDItZbJiBr9cEOaefilqJQSGhSybiB6SRFA8b-p86rFbiNEHjEo3w",
"results" : [
{
"geometry" : {
"location" : {
"lat" : 47.610399,
"lng" : -122.335791
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "8f7e7d038dbd13f283d1875ecf68d04fc2255561",
"name" : "GNC",
"opening_hours" : {
"open_now" : false
},
"place_id" : "ChIJ3zJ_hLRqkFQR0AxDtprgAjI",
"price_level" : 2,
"reference" : "CnRlAAAAoWRGwTPL4nLYI7qPZ2ukmXEdDWAScl5XASmeogPbQsOEvbFdXvb2cMYWsQZ1LR3QUYDo8djSpfKQc0BFLkA37iw_iTW2XRZy4568H5kReNd1xqVDjZyVmHPaWnKkJrTl0bJh_1P1Vpg-ntqUq-zVkRIQDVbTyrun8WsRA1eftayO8RoUSwjCBqkVaPwmyLJtXfJfk9qmF2I",
"scope" : "GOOGLE",
"types" : [ "health", "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "421 Pike St, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.61,
"lng" : -122.33
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "82e45a03dca8dcb2192cc250777a11f2e06452de",
"name" : "Deluxe Foods LLC",
"place_id" : "ChIJ9dKBxbVqkFQRoDLJeSe0CZo",
"reference" : "CoQBcwAAAD1dI5pifx1zH03cyQyPsJL10GZc7UFMHEKgZVzmIbZ77ZUdoXAme2iE3WzBgcCUuwg3zv735Pey9MrLWXdbKBmBGl_mvs_dD6aMmNYEhwft54gVpabn0ZVNLrflNEyun-dUkoeHvx5GEWNj1UvYGB1zbCY1CSUwbiqH4Ouay9GpEhBHwGUT7yCYw9IC3hGGUUxgGhRHU_AjT-jsRrHxoC6o7YKVpCLyCg",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "PO Box 30102, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.607573,
"lng" : -122.333167
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "695bf0d73be3b5f55ca43fe14abce48f9853ef15",
"name" : "Community Grocery",
"opening_hours" : {
"open_now" : false
},
"place_id" : "ChIJNeh4UrFqkFQRV3OXAHsHZxg",
"reference" : "CoQBcwAAALpG-2YSWEiceu-5h5km-SYuQelZR70nMSMg9uhs38uPusDF4s_5hnf6eQ8ORGJf7JGv9w9il4cL9to8r0bcA7HxK2ghxkrWlBbcP2asPWJTCdzrQB-uUs9TKxHc-kldayis9zmLQtHoDW_J8sSXlZHnHNYPol9VjKZIUiIlpYTNEhD9a-VcIRldCMVZhHNow03fGhRsw5lDMDZOcVEHdRBz2NAuhTdZ3Q",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "415 Seneca St, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.608163,
"lng" : -122.335371
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/wine-71.png",
"id" : "fde562e2d1e74e8ea18d3f758a05fe78c45d9346",
"name" : "Essential Market",
"opening_hours" : {
"open_now" : false
},
"place_id" : "ChIJvYQm8LNqkFQRjJM6WIcYE1A",
"rating" : 3.8,
"reference" : "CoQBcgAAALd33oAy_-Vxbcbf_gMywCqPHjyZixFi-yc1glcFFxbtpTDywxT-vPFKNExST7K305Ohv0AkhA_Br2Eh8AeetN-D7PxuW3mK5vsYPUbIfOTRWoM_CX2YH84cXOj9eFwGXwTiuXEOvOjhz8SYj59N4g78IUUDi-mfmGzd_C_jq90REhDJ3y45nfvr6IbfvapMnK0ZGhQDCjyU7Zu8ymTuQLGmoEsJRTVVGQ",
"scope" : "GOOGLE",
"types" : [
"liquor_store",
"grocery_or_supermarket",
"food",
"store",
"establishment"
],
"vicinity" : "1301 4th Ave, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.612122,
"lng" : -122.332044
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "6b4f8dc7039510bbbfca8fd12b359d444eef2be9",
"name" : "Clay's Market",
"place_id" : "ChIJdxjOE7VqkFQR4oDXOXnQEQ8",
"reference" : "CnRvAAAACC-kzX3pJo0x3OjptaM_wqWs5kbrk53V4xa6gYiOj3PP8Fo8L_DqSTjKRMqrF8bRGVfnTKtaLujZLvxkIgj7pNjyTAauRPKv0TL7dHpru66CyaJRNjoRSHME0xYHJJNj7VQDeLl5HO8dab0FZFrevxIQ6tZzbeqqRp6QEPf_Ue7XhBoU4Y3VkFBSCx_4b1pu1ApLYwXzLDQ",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "815 Pike St, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.609336,
"lng" : -122.337791
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "691548827b64fe2609fb7c27abbb1bcbe224f327",
"name" : "Kress IGA Supermarket",
"opening_hours" : {
"open_now" : false
},
"photos" : [
{
"height" : 600,
"html_attributions" : [],
"photo_reference" : "CnRnAAAAl-_QCXEqC8UaTUMrQDzG_bDcP56Lf2i2CPbBmxYj71VlemLiDrQpC38wmDTwsHvoFF_pGjLBo4vQHfPjM2nSLrPmHQeotYySYTwBhOcXPF9ISdvUNjBbyGUwCUFyBDvdcdIzaChOO0bEi5JVZRxNhRIQVsyOvUEat6gFROdcoh95HhoUS-Mac5cLzJFt12vSx_E7m_VUIfI",
"width" : 398
}
],
"place_id" : "ChIJbdIhoLNqkFQRMTiHCN6W4nU",
"rating" : 4.4,
"reference" : "CoQBdwAAAN3wdCAgdfatcILheBD3VwNP0Z_Zqjq8niNljnqpzHYJ2ieG6VEgxvfMQCU8BfawcRxO7MTu08gXtElThcYPdrQy5KFfAgdj1dFqi8PUh5R7wKKChjGN-sPj2cqRSIR-K853WjMlNMAhZSJMc2XWAhlbmy2AbXoe_9LsPoQfYE72EhCOBHIK_E0gTGPetycfRP6MGhSlBGpKKlZL00qldn6Xd44Y1TDj7A",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1427 3rd Ave, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.606546,
"lng" : -122.335449
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "3f23cf0c8bfdbe746a69d518c031dcafbd266d64",
"name" : "Netzel Associates",
"place_id" : "ChIJ1Q6vb7FqkFQR0PCOSJtLQpM",
"reference" : "CoQBdAAAABiMSr5ILBmkVjTeRpVAVHij7jMxhTacAJvz9YTFXVQYCP8he5wD4pAfaOu5dBlI4oagjaPPq8qUif9_hYN_IHTKpYnHaFWqCoHsHe-6kKIO1sIiE5qdLG-Buz2tQkVIPmzPqk67X60dgRux31vZjf10dg6BvizIiMTk5Q8D4AZWEhCJKbWcknUroTKKyatmH0JwGhS2i7O8iONEf9A5_qTBl3N1K8v4fg",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1111 3rd Ave # 2500, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.612607,
"lng" : -122.336984
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "1672b9dd46c6dcb5ae1409d0f87b23f1c38f33d0",
"name" : "Market Fresh Winter Garden",
"place_id" : "ChIJrdhDMksVkFQR0ugNKk-w7Fk",
"reference" : "CoQBfAAAAAxvLHmqlrTabI8g1bBkEMySU_pDqmYpmKmWNie37kRzknuKxRQkbPiVOs7cCB4yxiJGZ2X79qQW1dTFpJ9EE4jclrSKMFqzDqzPET1VHCR_imCSIWRLz_51iveURUW8r2GszBwCDISlu1iB1GBuLT58UB8Ghs-N3_yGxqaE_1JAEhBJn_a6kPPhJqGq9_pr4AifGhQNEEknt7JSyB1mvXCDgZ4o83aNng",
"scope" : "GOOGLE",
"types" : [
"meal_takeaway",
"restaurant",
"grocery_or_supermarket",
"food",
"store",
"establishment"
],
"vicinity" : "509 Olive Way, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.613069,
"lng" : -122.329411
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "08e8916d1ade44b3403b1a1268fc038bd449952b",
"name" : "Pike Grocery",
"opening_hours" : {
"open_now" : true
},
"photos" : [
{
"height" : 384,
"html_attributions" : [],
"photo_reference" : "CnRnAAAAmx9YeUnwUAcOesWmlueyy2ZJQT-GLpRSd0_WXWHV0pP7c42-FvQYcfkpRoDPCHiqlzJSWa_fPcqADGvAi_HaLFCcUhcIbbrTeJOSvlP1bA_ACdWmPBDF26vYc6xAJqmtR37EXiZ5j-sVYh09_hpEzRIQap8C87zKuwIYfsR6zsAg2RoUsAbkV4FLlFmsKTthHEbVdd0G9VM",
"width" : 512
}
],
"place_id" : "ChIJMwdemspqkFQRUqQ91N-ufeo",
"reference" : "CnRvAAAAPclUek8vRV5Wcsbp4G08nttAphmKfTASUhTAR4I8vUnTFkSp7NP06JC4y3Xj7qTr9I105d7HpVkQJhuH6HDyLzej-FM5yDc7LqNMmkrAN3CcmQezcd9MhSZkdXdTDKTsYaZV4GgWogi4lqFcUgfhDBIQd1M7Sq4-EWJ4Y6cNUJPKHBoUnHoVxfWyl3lK7imAxJkxGkKNH9k",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1011 Pike St, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.606521,
"lng" : -122.337704
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "110c00b34dc635662716772ab4a0e0f6deced28a",
"name" : "Young's Market",
"place_id" : "ChIJ44qIiLFqkFQRw1TsXz9H2kg",
"reference" : "CnRwAAAAxMdsj6FXUIP8S9c42j9nSu7XtMyNY612TS6njqubsoZLBJwpr6GmxsrRKREPttLtBSYWOmq4gP2ovwO2utvqtLpjHcWs2YCFhNKj4k8Sz7F_d3aoAzOwNKmIp88bPxMbPUJj9eKI5gvTxct9CfRe4BIQolTWxCAybHKlUTMnfVgNfhoUlO1jmn7wrjUBVBcnZYe5QubTl_w",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1210 1st Ave, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.608777,
"lng" : -122.33973
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "d2f9218e208a84da7c7a7e2980cebd3671a8f386",
"name" : "Double Dorjee",
"place_id" : "ChIJkX3s-LJqkFQRQRexqCN0jQY",
"reference" : "CnRvAAAAoRDVxZ8W_qmEWPmjxXulAz9lS562rRQgirkEIhgXesLoXNgcJsw2iQIymEUKRmvQwrAOuLYrOIa8t1yPQdYYAvEajqIxcZ0PWzMEUeaDuCKL37p_WGzKOJuHhv0ki_3Z10BOPwmCNvo5XiOfz7j1ABIQK9UdmY_KTIYTP8tnoG5T_RoULYEoZrNBYy8EULQD7LUGs_E7krU",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1501 Pike St, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.608248,
"lng" : -122.339523
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "efeff82fc657a72cf8a3079aabe0e27d0d91a6c5",
"name" : "Lina's Fruit & Produce",
"place_id" : "ChIJD22J-LJqkFQRxhEAGumCphs",
"reference" : "CoQBeAAAAFcDpYes7w9SDBBHP_bOYNQrM1CUQichslNR-hyXATeyWMptKnIEdS27q0HR5BLfQAzQvgP_yIM2ElDrfSjtSdTVgiJlWZuVhtsBRIdV4n8cthl6uihZJnmtevrBYRq7GEid2Bp_IZiLJ4K6nYIpsyKlJNpBBvcKASbEsn4TAtM-EhCYsE2fgYPwEgX4ZHAkU6t7GhS2-N0cGpHzmVMAgNMKYP2GCW23-g",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1431 1st Ave # 7, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.605109,
"lng" : -122.333955
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "d81b188da1c901ec10149c5224e6fd1beac9ef9c",
"name" : "Second Avenue Sundries",
"place_id" : "ChIJexG9_rBqkFQRi7Zb8KM4P98",
"reference" : "CoQBeQAAAOH07x4jtyj3WTa6sWNSHXCPGItPL7kbSJbQFreF2VUJ-vxjenChmIoGcvsIREYEsV_dZYyXG3RoMpezUCUkuIvLsaEt1wxJcK6t0tM8QHylTdcc9IJRmjE_npy9rTbIehBixa5ESV1EGmF_rhyHQJyhPjAz7hmDcdlysEBRuySsEhAq3UlVuENA0aC8L59q8ZEFGhSUrFU9dmUkkl24SQ6RMQvA74mB5w",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "999 3rd Ave, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.614062,
"lng" : -122.335451
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "69ff94699431b37223bdea4fd7792c7244a6b36c",
"name" : "Sphere Foods",
"place_id" : "ChIJ1ef53EoVkFQRlYzOwPY7DhA",
"reference" : "CnRuAAAA6qjCWZS9h_Eb5x5UOAN2J99LIsJ9NEVR-LtbjUoLKx5OJmLmGGYEazQmbf02Rp8TrqyEVeE41rHimDGdvnQkj5WbLrxQCVM195Y3bLssymy7kUTFVaiclgZjVdEPf5WLcC9Xl6JjfiWxqN-5I1oEihIQZtGMYRH317G1c7rRQWd9MxoUmSFfTX5FwgECzfDH7hRFX4F5mJA",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1700 7th Ave # 2100, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.614425,
"lng" : -122.334307
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png",
"id" : "e5be7cde6eb52cb0a5a7aa975ea915e2f80bd7dc",
"name" : "Stewart Street Market",
"opening_hours" : {
"open_now" : true
},
"photos" : [
{
"height" : 1152,
"html_attributions" : [],
"photo_reference" : "CnRnAAAAqeZQn6gaLGIJib2WaNAcw7GdNLJuCc6AvrMCF543GBpl9tgLJ99CS0DmrdbHyWVMcw_H7q_LZgOqqKLwkilB95SjXsJqu4O3KMVQQAqtQWHW27u2vKVkTOqxAWTDppdManOPMUlFP2LLN1zya33z2xIQY-si5NTVqW9CpTd2AhP9NxoURwp9wSSgmZY2dtC1q6OezNsUvQw",
"width" : 2048
}
],
"place_id" : "ChIJ7yBfx0oVkFQRcLjVXNF_3qg",
"reference" : "CoQBeAAAADTkEpb503o9qN93lxGtIlh8x-uyIgRIs0Pj_-gGXpxQoQkAnIGcmVYgDLEzcFtpgzDchjxRWmns1RPUhqnThNJp4_VmG3ZP42zFNDAO_Q2_YowPiM7Wgz3oQsr66uIEBKZnNW9QRrd_GMbA64OdLZGrn7wZwvdp5UWCCnJJe0NFEhBKdwGNlHZTu2Y4BOi6Zl-lGhSbbDS4npeTaLx_cwgpmTmKUpp43g",
"scope" : "GOOGLE",
"types" : [
"convenience_store",
"grocery_or_supermarket",
"food",
"store",
"establishment"
],
"vicinity" : "1812 8th Ave, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.609322,
"lng" : -122.325655
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "87b467979468e6d2b5b203c7486d84e08505dd17",
"name" : "Plaza Select Foods",
"photos" : [
{
"height" : 720,
"html_attributions" : [ "From a Google User" ],
"photo_reference" : "CnRnAAAAhzdJBafPaRrh7mLSuB87bVza6ErJKRSvyNKU6pGa1EcblihzmqNx67HZQog-EO7wkvag7KP0bNXWvNXo9RF5u2vYNxZZ-5v7Z7KhL7tOmfWi7U7n6aFr9Sls_fZ_KcpBmH-m63Nl4H6E5GDFZwwNuxIQJUHOGAtXDDa9YT62KVkEjhoUVenpQc8oBGHYJOwTPiJDxSf2aH8",
"width" : 960
}
],
"place_id" : "ChIJy1bFH7ZqkFQRXpKoZ2NFh2Y",
"reference" : "CoQBdAAAAJZ0WNOu_ProGJ7t62FbE2ADOx6sGGH0YIR1KNGRqiZ9scWKjBjMhUHb3VNKZzEuvFUIBPOjrfBtkbjpEUthCC8HHbe_hsuLCyp2FUi2AFDVgBWB49X4fPla0IcNoOECfEzwRskswcKgcNyob1wt_mB2nEEbQVFDr8W6vhRsl1EyEhBgAGQ9WXQzLHZzzRs84JEyGhRM0Rx5TiF0g11uFT5mg6v6uHr97g",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1024 Madison St, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.608802,
"lng" : -122.340552
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "14a2e0601f06734e7c8be82ed00d3806e3f9ecd4",
"name" : "Corner Produce",
"place_id" : "ChIJn9FR9rJqkFQR89xobVdunBE",
"reference" : "CnRwAAAAFad6cCCAww55VmSDqvH9XCZzHx5LoyFH2MHT5YkFM9NZNr7L3eKhd02_VnSEunZ3sMlCyvT37k-Tg6EZV4MuWVOZ2imbVrPX1OeRgJWS6OgXx-FHabcAj2W5jgRkCsHwEhjwTW0DNYNDX5GEn-a1HRIQNsCEN53Q1PC3NAHCTutgARoU-4Qq8ZOFJfF3Lyyc1Xf66FN8KQ0",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1500 Pike Pl, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.60884,
"lng" : -122.340616
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png",
"id" : "2a7a890254bcb2b1ae409cbf78d4e741800f036f",
"name" : "Oriental Mart",
"photos" : [
{
"height" : 460,
"html_attributions" : [
"\u003ca href=\"https://plus.google.com/115115924044576716973\"\u003eMatt Good\u003c/a\u003e"
],
"photo_reference" : "CnRtAAAAYab-XNz1Tm-iiB1qfxZJiC-T--LI7Mr5haR62p-RlGTrAIAPk2lhWogWOTQcFjPIHCiM2aYN2Iu-qkqPVGsEGony955zdEo6T3oOUKgNvT5WZvw6w_WU3aNdyLNh0xh-wRi4TNi6T70jutWPs6qkrRIQbdKBlQ6Yk1ROLxYnfSYF0BoUn9chrbaMj73dJleIqEzjxmi-9nY",
"width" : 816
}
],
"place_id" : "ChIJt7FX9rJqkFQR-R2iORwxyXQ",
"reference" : "CnRvAAAA8b_D2XlfYEVl6Oi5uYGRdJILg6dW3oRrLu8Q9xd9EukH2omaTGNlupKgahjJREN5jiNKdj9FSmpR4F0Du2XMbW9xmMEpO6CmPEG8zbBHQVNBs2zHzU2_m0WFnLv21mdxfAd20GVX6f6cdZ2COkb_KhIQhh6kZ-vny430yF0EvmH7eBoUZ3TVjq_RHxXlY_jpaN1qk2Yc3UM",
"scope" : "GOOGLE",
"types" : [
"convenience_store",
"grocery_or_supermarket",
"food",
"store",
"establishment"
],
"vicinity" : "1506 Pike Pl #509, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.608918,
"lng" : -122.340729
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "301d23a6579067833036e39ef46357e1d8e5ba68",
"name" : "Frank's Quality Produce",
"place_id" : "ChIJpdHb8LJqkFQRo8X7RoXoz2U",
"reference" : "CoQBeQAAAKEBHFRM9OPUb4CWC5bC1HYamXOFc0lzWyWCX-4eyhtOOIEvcxHIsj4y1xkBiXTIsttPetJYNr5KKux3Y3q5ZF7SKutTqiRYbk0hESohet1_EdcnPU31_IBRLo9W-T_7wiDb3FnN24rYmz9J9Kz7pxhlyKyR2VLMuTLMALNLJC2YEhChDDy463zp0HRiZaAsFSG3GhTe1n0deejJDnxNB2AK4zOf1HGkMw",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1508 Pike Pl, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.609231,
"lng" : -122.340848
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "228c792a04f0d9c3b8086b9f857521945f87c975",
"name" : "Choice Produce",
"place_id" : "ChIJmbBY97JqkFQR4JkuKXVDT3A",
"reference" : "CnRwAAAAWE1aK6eOe-rdRjgs2HJZHB__viCdWu2u7Qx0chME1CgDnnOycW_dcim98Szb2bzUjTy12wGk9fGO42jpywL-Zevm_ilfqi6q7jvRQxSGvsybbZau_Tc_orGj41Gz8fWK1R6_YvAT1n0II9DPFca1lxIQviRIxzWL3GRqyK3dun_4zRoUrNDpGHpGZHKUngGPiBLLH46zOqI",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1514 Pike Pl, Seattle"
}
],
"status" : "OK"
}
I need to parse it and get the lat and long values
I have assigned it like this
JSONArray JA=new JSONArray(result);
JSONObject json= null;
Where result contains the web service response. How can I parse it and get the required values?
try this code.you will get lat and lng value:
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
results = jsonObj.getJSONArray("result");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
JSONObject geo = c.getJSONObject("geomatrics");
JSONObject place = geo.getJSONObject("location");
String id = place.getString(lat);
String name = place.getString(lng);
}}}
FYI, the response JSON is an Object, not an Array.
Steps to get lat/lng values:
Get results JSON array from received JSON Object response
Iterate through all the sub JSON Objects, then first get geometry sub object.
From geometry sub object, get location object. From location object, parse and get lat/lng values.
Please don't ask for code as there are many JSON tutorials exist on web but you can use below methods for parsing:
getJSONObject()
getString()
getLong()
There are multiple locations in the JSON so you will need and ArrayList to store all of them.
I recomment using ArrayList<HashMap<String,Double>>
Assuming you have the result as a String object named as result:
ArrayList<HashMap<String, Double>> list = new ArrayList<HashMap<String,Double>>();
JSONObject jsonObj= new JSONObject(result);
JSONArray results = jsonObj.getJSONArray("results");
for(int i=0;i<results.length();i++){
JSONObject temp = results.getJSONObject(i).getJSONObject("geometry").getJSONObject("location");
Double lat = temp.getDouble("lat");
Double lng = temp.getDouble("lng");
HashMap map = new HashMap<String, Double>();
map.put("lat",lat);
map.put("lng",lng);
list.add(map);
}
now you can retrive any position by using
Double lat=list.get(4).get("lat");
Double lng=list.get(4).get("lng");
Hope it helps. cheers :)
Cast the response to JsonObject like this
JSONObject jsonObj = new JSONObject(result);// where result is the response you got
Get the result array like this
JSONArray results = jsonObj.getJSONArray("results"); // where results is the tag in resposne
Now you have the entire array in results
for loop the entire stuff
for(int i=0;i<results.length();i++)
{
JSONObject location = results.getJSONObject(i).getJSONObject("geometry").getJSONObject("location");;
String lat = location.optString("lat");
String lng = location.optString("lng");
}
If you need the lat and long inside an array, add those to an array.
My simple android application uses google places api to get the latitude and longitude of a place. I am getting the json response from the google api as below
{
"results" : [
{
"address_components" : [
{
"long_name" : "Chennai",
"short_name" : "Chennai",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Chennai",
"short_name" : "Chennai",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Chennai, Tamil Nadu, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 13.23408510,
"lng" : 80.33229129999999
},
"southwest" : {
"lat" : 12.94592670,
"lng" : 80.14878270
}
},
"location" : {
"lat" : 13.0604220,
"lng" : 80.2495830
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 13.14736150,
"lng" : 80.37764240
},
"southwest" : {
"lat" : 12.97345190,
"lng" : 80.12152360
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
Now i want to get the this part in the json response,
"location" : {
"lat" : 13.0604220,
"lng" : 80.2495830
}
I only get the whole "geometry" part as string through this method
JSONObject e = address.getJSONObject(0);
e.getString("geometry")
How to use the only location values. Any help is appreciated. I'm not sure what is going wrong.
JSONObject e = address.getJSONObject(0);
JSONObject jsonLocation = e.getJSONObject("geometry").getJSONObject("location");
and use
String lat = jsonLocation.getString("lat");
String lng = jsonLocation.getString("lng");
to get latitude and longitude.
You can also get location values as follows:
For Longitude:
Double lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
For Latitude:
Double lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
Try this. It worked for me.
JSONObject result = jsonObject.getJSONObject("result");
Double lat = result.getJSONObject("geometry").getJSONObject("location").getDouble("lat");
Double lng = result.getJSONObject("geometry").getJSONObject("location").getDouble("lng");
I'm writing an android app that takes your current location and returns a list of the stores near you using the Google Places web service. I'm able to get back (what I think is) valid JSON, but when I go to parse the JSON using the Android JSONObject library, I get a parsing error.
Here is my class:
public class StoresNearMe {
private String result = "";
private String googleAPIKey = "Omitted for this example";
private String searchRadius = "50";
private Location myLocation;
final String TAG = getClass().getSimpleName();
public StoresNearMe(Location location){
myLocation = location;
}//StoresNearMe
public String getStoresNearMe(){
String result = "Nothing";
result = callGoogleWebService(buildURLForGooglePlaces(myLocation));
convertJSONtoArray(result);
return result;
}//getStoresNearMe
private String callGoogleWebService(String url){
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
//request.addHeader("deviceId", deviceId);
ResponseHandler<String> handler = new BasicResponseHandler();
try {
result = httpclient.execute(request, handler);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
httpclient.getConnectionManager().shutdown();
return result;
}//callWebService
private String buildURLForGooglePlaces(Location location){
String baseUrl = "https://maps.googleapis.com/maps/api/place/search/json?";
String lat = String.valueOf(location.getLatitude());
String lon = String.valueOf(location.getLongitude());
String url = baseUrl + "location=" + lat + "," + lon + "&" +
"radius=" + searchRadius + "&" + "sensor=false" +
"&" + "key=" + googleAPIKey;
Log.d(TAG,url);
return url;
}//buildURLForGooglePlaces
private void convertJSONtoArray(String rawJSON){
try {
JSONObject completeJSONObj = new JSONObject(rawJSON);
String json = completeJSONObj.toString();
Log.d(TAG,json);
JSONObject results = completeJSONObj.getJSONObject("results");
} catch (JSONException e) {
Log.d(TAG,"JSON parsing error - fix it:" + e.getMessage());
}
}//convertJSONtoArray
}//StoresNearMe
Here is the JSON returned by my call:
{
"html_attributions" : [
"Listings by \u003ca href=\"http://www.yellowpages.com.au/\"\u003eYellow Pages\u003c/a\u003e"
],
"results" : [
{
"geometry" : {
"location" : {
"lat" : -33.86694910,
"lng" : 151.19549870
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
"id" : "96760d4544ecdaaf2e87565915638238ca960f20",
"name" : "Pirrama Rd",
"reference" : "CnRqAAAAL0xKaK8vGlVlD-E5j-88K9YTuCKlOFfoBxhBUwMIBGxNQDC8UdkULJiiC0ICBRxpddCV09wyc-HtShEpquP-drX4sj4tI_tbfveDIbzfSAUaNQjYhhfjQ-eVi0VeWBXdXH0A8Z_-nKXKMo1fvCHtBhIQlAeLnFtotWnYonMUpU1DqxoUcI1weYioNnJPtnhXCThmx0W0ubU",
"types" : [ "route" ],
"vicinity" : "Pyrmont"
},
{
"geometry" : {
"location" : {
"lat" : -33.8669710,
"lng" : 151.1958750
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "4f89212bf76dde31f092cfc14d7506555d85b5c7",
"name" : "Google Sydney",
"reference" : "CnRkAAAAqvesgoigCyiZfDUwqjbravZHH5zsXrmskbQVhkFk1JMnnrnKNGQyoHBBYofTML_zZqaAUAKJeJCvReUn62oGzSgPgNSjWaXUVorKGDXAAjhGyeh2pRrJFwuC5qsgBYdyN4kY8kspJGu_bMo3xrranxIQ-mFIK_28TAqiagOAzY7RiBoUrZTvFJYSn0OXimLRRb_SQ3OKW44",
"types" : [ "establishment" ],
"vicinity" : "Pirrama Road, Pyrmont"
},
{
"geometry" : {
"location" : {
"lat" : -33.8670840,
"lng" : 151.195950
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "27e84d1acda19c14b9c7f5271242796891bc9c9e",
"name" : "Accenture",
"reference" : "CoQBfQAAAOpBriMi1e4itUquHQnv_jQyEOibrEnbWfAuJBJqBUzcTqNvY6luCcgiqHr3ensEO81bcTkt4fXGHFLxM7pERmxVvFnRnLsLy5xyQtVxIp8eQnYP6AZxLZyxbfGceG87r6A1lHCOQTFxK85HVFq7pYik9ud_ExxEtwHT3gO54pihEhDYEZ59AYpLMDmlRHk5__q_GhQdTS2wSOU2sAxy0j4J7EYwCuq6Iw",
"types" : [ "establishment" ],
"vicinity" : "Pirrama Road, Pyrmont"
},
{
"geometry" : {
"location" : {
"lat" : -33.8670520,
"lng" : 151.1957360
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "3ef986cd56bb3408bc1cf394f3dad9657c1d30f6",
"name" : "Doltone House - Darling Island Wharf",
"reference" : "CoQBewAAAExUq-YlS-bahqAJeJbfyGfkUrqyuKjZgCPxHznMZsHg1aHLWoFk8s_04DxDtEFAlSyRP-ZHNsceha6MB8JD7zFMjbYOKNJLSm9OcgFUOFB-4SZP3ZeRyYzna2032gayzUU1_8N-qtb_r2kfllGYAmgnaU7h6RC8haaf-dFfvdiiEhBUdWHpEeL7qwtB91BgNYdPGhTAjf0Xa7-MKHJRRCQu_g7XL80dzQ",
"types" : [ "establishment" ],
"vicinity" : "Pirrama Road, Pyrmont"
},
{
"geometry" : {
"location" : {
"lat" : -33.8671110,
"lng" : 151.1958750
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "258f23471a33cfa2828170660d94cd5797edb18c",
"name" : "Workplace6",
"reference" : "CnRhAAAAi_hm5c3gpntkNvC7NbQtIe5tcmosCIHHbzqhLURaH7mQ6inEOqg47_cltBrtFBEPSIqCuPQDC3UA-suLBfRslIwUNytKjC87Lt1ZrGXF6ZmnOR2rVcdCGdoWrdi977RHruBO21zbjzVWE-A3ZDn2NBIQ1Vu2-nr3E02DuJAtXL8x4BoUEBRWEHReK2LO3IoldvyyvSKb0Qs",
"types" : [ "establishment" ],
"vicinity" : "Pirrama Rd, Pyrmont"
},
{
"geometry" : {
"location" : {
"lat" : -33.8670520,
"lng" : 151.1957360
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "fe9136d6406ed2d35660dea37a0be34043c1a4f2",
"name" : "Doltone House",
"reference" : "CnRjAAAAo4zx1lM-veFEPBBeIRFETZoH8QRMmpHIBVtnmfOZ-Sh1-8p4vxKj-146pHGW6wPCZxIV0iIuAGow9OSsHubvY_kSnlBe5OB99ZSxHQsm0LrLh45c05Gf7C8jwp_9D7keDVDAC47MN65niSZW71fOrRIQxODppLktQZFWbf-V1N4gHxoUPetbj0zbU1kQj96YrRLzLKrbMoE",
"types" : [ "establishment" ],
"vicinity" : "Pirrama Road, Pyrmont"
},
{
"geometry" : {
"location" : {
"lat" : -33.8666470,
"lng" : 151.195650
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "2722b46120160ec0a322e0a13cb0717ed41a696b",
"name" : "Biaggio",
"reference" : "CmRdAAAAp7wiB1b9cK-26FjhO2DVAGA-5uzuZxppHqCkR75dQxoMJiLD5G7H90_dKtNVDlRAdKSOx7hWlUoHGuwkbBA4ZhNIDrPdLWQ1a41sEN9gZJvU-t7uxoRzziZpTVSCU-AXEhDoQ36icc3OBaMH1xxnPgsEGhTB08nD6Q67ojrGYMJWrd4PojJKsw",
"types" : [ "establishment" ],
"vicinity" : "Pirrama Rd, Pyrmont"
},
{
"geometry" : {
"location" : {
"lat" : -33.86954560,
"lng" : 151.19454040
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
"id" : "40840d476baa531e0227a353b1bef70262f66e7e",
"name" : "Pyrmont",
"reference" : "CnRnAAAA7aHZwg2JyjTI-xotS1q4XKgbg3eCUwOZm3Nz55Xf5Q2w-nZpBrrd2VAEl60On0puVl8bX7UaJTIECELr3KY2mL4s7zsIznItOiJEG-9QMlnreRSmxSBSUAkPghKWoq4OFbCr0dwNtZSKJFazcbCc1BIQSz5BZfCm-iyMZ8YOgvc4tBoU1CdnwsHS5PhB69jpYHsOvfWXEgo",
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
Any help is appreciated - I'm not sure what is going wrong.
Thanks!
The problem is that you're trying to read an array in as an object.
Change
JSONObject results = completeJSONObj.getJSONObject("results");
to
JSONArray results = completeJSONObj.getJSONArray("results");
and the parse exception goes away.
your JSON should be Valid : example :
{\"key\":\"value\" }