Sometimes I get a different line. Key: value in Json Array

Sometimes I have another string tag in the Json Array like

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

         "name": "V & P FOX LOCKSMITHS",
         "opening_hours": {
             "open_now": true
         },
         "vicinity": "91A Saint Martin 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"
     }] }

      

The analysis result is location dependent, when I used a specific UK location. In the parsing, I got three attributes. Name, City, Opening hours.

But when the location changed from UK to US, I only had two attributes: Name, City. There were no running hours, but I used three variables in my code for the entire location. But when we only got two values ​​from the web service I got an error parsing

My Java Code

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

      

What changes should there be when I got nothing for the "openNOw" tag

Three ways

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

      

+3


source to share


1 answer


check if in your object "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");
    }
}

      

+2


source







All Articles