0

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 .. :)

mike20132013
  • 5,357
  • 3
  • 31
  • 41

1 Answers1

1

You can loop through the array and get the value based on the index

 for(int i=0;i<jsonarray.length().i++)
 {
        String value = (String) jsonarray.get(i); // Note the cast
 } 

You can also use getString(i) or getInt(i)

Instead of this

 String value = obj.getString("types"); 

Have

 JSONArray jsonarray = obj.getJSONArray("types");

 for(int j=0;j<jsonarray.length();j++)
 {
        String value =  jsonarray.getString(j); 
 }  

Edit 2:

Have

 ArrayList<HashMap<String,String>> list = new  ArrayList<HashMap<String,String>>();

Then in for loop

 HashMap<String,String> map = new HashMap<String,String>():
 map.put("key",value);
 list.add(map)

TO get

HashMap<String,String> map =  (HashMap<String,String> ) list.get(0);// 0 is the index of item
String value =(String)map.get("key");
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Cant use JSONArray jsonarray = obj.getJSONArray("types") since "types" is a part of the results array. The thing is, I am able to get the values with my code too but again, its all wrapped in the braces. – mike20132013 Jun 24 '14 at 02:49
  • @mike20132013 types is a jsonarray so loop through it and get the values see the edited part and it should work fine. Log the value of string and check. WIthout braces – Raghunandan Jun 24 '14 at 02:51
  • @mike20132013 you have a json object node` after results array and where does that close/end?? ends here `"vicinity":"Some Address" }` – Raghunandan Jun 24 '14 at 02:52
  • The json provided is just a JSON Sample. I edited my Question and I am getting the values. But still not clear with the concept of parsing the json without it's key. – mike20132013 Jun 24 '14 at 03:02
  • @mike20132013 its simple `[` represents json array node and `{` represetns json object node. You get the value with key if you have a key pair. What you have is a types array with just values. loop through the array and get the value. What is that you still don't understand?? – Raghunandan Jun 24 '14 at 03:04
  • @mike20132013 just think that it is just an array of String – Rod_Algonquin Jun 24 '14 at 03:05
  • @mike20132013 you can use a arraylist of hashmap and add items. Once you get the values its upto you to handle the data how you want – Raghunandan Jun 24 '14 at 03:06
  • Thanks for the info. I got the concept. Now, after getting the values as in the code, how will I separate the values from other json blocks ? Because, it's like you are getting a set of results. – mike20132013 Jun 24 '14 at 03:08
  • @mike20132013 and check edit2 if you still don't get it then i can do no more – Raghunandan Jun 24 '14 at 03:12