Issue parsing JSON array without key

风格不统一 提交于 2020-01-07 08:22:29

问题


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


回答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");


来源:https://stackoverflow.com/questions/24377479/issue-parsing-json-array-without-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!