有时候我得到了不同的字符串Key:Json Array中的值

时间:2013-01-28 11:15:01

标签: android json getjson

有时我在Json数组中有不同的字符串标记,例如

 {
 "html_attributions": [],
 "results": [
     {

         "name": "V & P FOX LOCKSMITHS",
         "opening_hours": {
             "open_now": true
         },
         "vicinity": "91A Saint Martin's Lane, London"
     },
     {
         "id": "c680cf44d85a15cdc727e0101f8531db70c0cf1c",            
           "name": "London United Kingdom",
         "vicinity": "41-43 Wardour Street, London" 

        // Here i do not get open hours tag, what should do when we 
         //not get json parsing in synchrozied manner 

     },
     {

         "name": "Timpson",
         "opening_hours": {
             "open_now": true
         },
         "vicinity": "40 Villiers Street, Charing Cross"
     }] }

当我使用某个特定位置作为英国时,解析结果取决于位置。 我在解析时有三个属性。名称,城市,营业时间。

但是当改变从英国到美国的位置时,我只有两个属性 姓名,城市。没有开放时间,但我在我的代码中使用了三个变量用于所有位置。但是当我们从Web服务中获得两个值时,我在解析时遇到错误

我的Java代码

 JSONObject json = jParser.getJSONFromUrl(url);  
 try {  
    // Getting Array of Contacts  
     towLogSmithJsonArray = json.getJSONArray(TAG_RESULTS_LOG_SMITH);  
    if(towLogSmithJsonArray!=null)  
    {  
            // looping through All Contacts  
        for(int i = 0; i < towLogSmithJsonArray.length(); i++)  
               {  
                    JSONObject d = towLogSmithJsonArray.getJSONObject(i);  
                    // Storing each json item in variable  
                    String openNOW="";  

                    String nameCarRental = d.getString(TAG_NAME_LOG_SMITH);             
             String cityAddress = d.getString(TAG_CITY_LOCATION_LOG);  

             // JSONObject phone = d.getJSONObject(TAG_OPENING_HOURS);  
             //    openNOW = phone.getString(TAG_OPEN_NOW);         

             Log.e("nameCarREntal", nameCarRental);
             Log.e("CityAddress",cityAddress);
             //Log.e("OPenNow",openNOW);

             // creating new HashMap
             HashMap<String, String> mapLockSmith = new HashMap<String, String>();              
             // adding each child node to HashMap key => value

             mapLockSmith.put(TAG_NAME_LOG_SMITH, nameCarRental);
             mapLockSmith.put(TAG_OPEN_NOW, openNOW);
             mapLockSmith.put(TAG_CITY_LOCATION_LOG, cityAddress);

             // adding HashList to ArrayList
             towLogSmithList.add(mapLockSmith);
             }
          }
         else
                {  
            Log.d("LOck Smith parsing NUll: ", "null");  
            Toast.makeText(LockSmith.this,"There is not data for particular location",Toast.LENGTH_LONG).show();  
        }    
        }catch (JSONException e) {    
    e.printStackTrace();  
 }  

当我没有为“openNOw”标记

获得任何内容时,应该有什么变化

三种方式

 -OpenNow: True
 -OPenNow: False
 -“opening_hours” Tag  is not available in Pasring array  code.

1 个答案:

答案 0 :(得分:2)

检查对象中是否显示"opening_hours""open_now"

String openNOW = "";
if(d.has("opening_hours"){
    JSONObject json2 = d.getJSONObject("opening_hours");
    if(json2.has("open_now"){
        openNOW=json2.getString("open_now");
    }
}