在没有密钥的情况下解析JSON数组

时间:2014-06-24 02:32:43

标签: android json google-places-api arrays

我正在尝试使用Google Places API,而我基本上是在没有json中特定位置的密钥来解析Json。

我能够解析所有数据并根据需要获取值。 虽然,我在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"
}

现在,这是代码段,以便从我遇到问题的位置获取值:

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


            }

在这里,代码工作正常,我能够获取数据。

主要问题是结果格式。这是一个示例:

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”,“establishment”]

有没有更好的方法来解析上面的json,或者有什么方法可以替换字符和大括号 [“car_repair”,“gas_station”,“建立”]

就像:

car_repair,gas_station,establishment 以便我可以在任何其他视图的textview中使用它而不显示那些大括号和其他字符。

修改

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

在此之后,这是我得到的结果:

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数组分开。

任何帮助都将非常感谢..谢谢.. :))

1 个答案:

答案 0 :(得分:1)

您可以遍历数组并根据索引获取值

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

您还可以使用getString(i)getInt(i)

而不是这个

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

然后进入for循环

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