Sometimes i got different string tag in Json Array for example
{
"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"
}] }
Parsing result depend upon location when I used some particular location as UK.
I got three attribute in parsing. Name , City, Opening Hours.
But when changed the location from UK to USA, I got only two attribute as
Name ,City. Not got opening hours, but I used three variable in my code for all location. But when we got only two value from web service, I got error in parsing
My Java Code is
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 be there , when i got nothing for “openNOw” Tag
Three ways
-OpenNow: True -OPenNow: False -“opening_hours” Tag is not available in Pasring array code.
check if the
"opening_hours"and"open_now"is presented in your object: