3

例如,有时我在 Json Array 中有不同的字符串标签

 {
 "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.
4

1 回答 1

2

检查"opening_hours"and"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");
    }
}
于 2013-01-28T11:22:59.433 回答