问题解析JSON阵列无钥匙 [英] Issue parsing JSON array without key

查看:192
本文介绍了问题解析JSON阵列无钥匙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用谷歌Places API的,我基本上是试图解析JSON,而不在JSON的特定位置的关键。

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.

我能够解析所有的数据,并根据需要得到的值。
虽然,我有与该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.

这里的JSON示例:

{
   "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"
}

现在,这里的 code段以获得从那里我有麻烦的值:

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);
                            }


            }

在这里,code是工作完美,我能够得到的数据。

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"]

正如你所看到的,结果我以这种形式获得: [car_repair,gas_station,建立]

有没有更好的方法来分析上面的JSON或有什么办法,我可以代替从人物和大括号
[car_repair,gas_station,建立]

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"]

要就像:

car_repair,gas_station,建立,这样我可以在我的任何其他观点的TextView使用它没有显示这些括号和其他字符。

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.

编辑:

    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

但问题是,我怎么从他们尊敬的JSON数组分隔值。

But the problem is, how do I separate the values from their respected json array.

任何帮助将是非常美联社preciated ..谢谢..:)

Any help will be really appreciated.. Thanks .. :)

推荐答案

您可以通过数组循环并获得基于该指数值

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
 } 

您也可以使用的getString(我)调用getInt(我)

取而代之的是

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

有无

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

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

编辑2:

有无

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

然后在循环

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

要获得

HashMap<String,String> map =  (HashMap<String,String> ) list.get(0);// 0 is the index of item
String value =(String)map.get("key");

这篇关于问题解析JSON阵列无钥匙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆