I am trying to use Google Places API and I am basically trying to parse the Json without the key in a specific position in the json.
I am able to parse all the data and getting the values as required. Although, I am having issues with one of the parts in that json.
Here's the json sample :
{
"html_attributions":[],
"results":[
{
"geometry":{
"location":{
"lat":somelat,
"lng":somelng
}
},
"icon":"someicon",
"id":"someid",
"name":"somename",
"reference":"some_image_reference",
"types":[
"gas_station",
"establishment"
],
"vicinity":"Some Address"
}
],
"status":"OK"
}
Now, here's the code snippet to get the values from where I am having trouble:
JSONObject mainJSONObject = new JSONObject(mAppUtils.loadJSON(gasURL));
JSONArray mainJSONArray = mainJSONObject.getJSONArray("results");
for(int i = 0 ; i<mainJSONArray.length();i++){
JSONObject obj = mainJSONArray.getJSONObject(i);
if(obj!=null){
String value = obj.getString("types");
Log.d("ARRAY VALUE : ", value);
}
}
Here, the code is working perfectly and I am able to get the data.
The main issue is the result format. Here's a sample :
06-23 22:03:51.762: D/ARRAY VALUE :(8074): ["car_repair","gas_station","establishment"]
06-23 22:03:51.782: D/ARRAY VALUE :(8074): ["convenience_store","food","store","car_repair","gas_station","establishment"]
06-23 22:03:51.782: D/ARRAY VALUE :(8074): ["gas_station","establishment"]
As you can see, the result I am getting in this format : ["car_repair","gas_station","establishment"]
Is there a better way to parse the above json or is there any way I can replace the characters and braces from ["car_repair","gas_station","establishment"]
to just like :
car_repair,gas_station,establishment so that I can use it in my textview of any other view without showing those braces and other characters.
EDIT:
JSONObject mainJSONObject = new JSONObject(mAppUtils.loadJSON(gasURL));
JSONArray mainJSONArray = mainJSONObject.getJSONArray("results");
for(int j=0;j<mainJSONArray.length();j++)
{
JSONObject jsonObject = mainJSONArray.getJSONObject(j);
JSONArray jsonArray = jsonObject.getJSONArray("types");
for(int ju=0;ju<jsonArray.length();ju++)
{
value = jsonArray.getString(ju);
Log.d("PLACE TYPES ARRAY VALUE : ", value);
}
After this, here's the result I got :
06-23 22:53:44.928: D/PLACE TYPES ARRAY VALUE :(16322): car_repair
06-23 22:53:44.928: D/PLACE TYPES ARRAY VALUE :(16322): gas_station
06-23 22:53:44.928: D/PLACE TYPES ARRAY VALUE :(16322): establishment
06-23 22:53:44.928: D/PLACE TYPES ARRAY VALUE :(16322): convenience_store
06-23 22:53:44.938: D/PLACE TYPES ARRAY VALUE :(16322): food
06-23 22:53:44.938: D/PLACE TYPES ARRAY VALUE :(16322): store
06-23 22:53:44.938: D/PLACE TYPES ARRAY VALUE :(16322): car_repair
06-23 22:53:44.938: D/PLACE TYPES ARRAY VALUE :(16322): gas_station
06-23 22:53:44.938: D/PLACE TYPES ARRAY VALUE :(16322): establishment
06-23 22:53:44.938: D/PLACE TYPES ARRAY VALUE :(16322): gas_station
06-23 22:53:44.938: D/PLACE TYPES ARRAY VALUE :(16322): establishment
But the problem is, how do I separate the values from their respected json array.
Any help will be really appreciated.. Thanks .. :)