Android 有时我在Json数组中得到不同的字符串Key:Value

Android 有时我在Json数组中得到不同的字符串Key:Value,android,json,getjson,Android,Json,Getjson,例如,有时候我在Json数组中得到不同的字符串标记 { "html_attributions": [], "results": [ { "name": "V & P FOX LOCKSMITHS", "opening_hours": { "open_now": true }, "vicinity": "91A Saint Martin's Lane, London"

例如,有时候我在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"
     }] }
当我使用某个特定位置作为UK时,解析结果取决于位置。 我在解析中得到了三个属性。姓名、城市、开放时间

但是当把地点从英国改为美国时,我只得到了两个属性 名字,城市。没有开放时间,但我在代码中对所有位置使用了三个变量。但当我们从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();  
 }  
检查对象中是否显示了
“开放时间”
“立即开放”

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

嗨,我有个问题,我能用字符串打开吗;在自己的项目中,如果(phone.equals(“”){openNOW=“NULL”;Log.e(“如果开放时间”,openNOW);}else{openNOW=phone.getString(TAG_OPEN_NOW_Log);Log.e(“else开放时间”,openNOW);},但我如何在字符串中保存迭代字符串,正如我在问题中给出的那样。请帮忙谢谢我不明白。。?你想达到什么目的?我无法访问此url。您应该检查元素是否存在于当前对象中。这是
has
方法。检查答案。谢谢@mihail我找到了答案。
String openNOW = "";
if(d.has("opening_hours"){
    JSONObject json2 = d.getJSONObject("opening_hours");
    if(json2.has("open_now"){
        openNOW=json2.getString("open_now");
    }
}