java - Unable to extract out name in 4 level deep nested JSON array -
i trying create android app shows details restaurants. have parsejson function takes string json argument. trying show name of individual restaurants in json below. able extract individual restaurant(3 levels deep) , print them out through log.d console not able extract out individual restaurant's name. here code
public void parsejson(string jsonquery){ //parsing json try { jsonobject jsonobject = new jsonobject(jsonquery); if (jsonobject != null){ jsonarray restaurants = jsonobject.getjsonarray("restaurants"); (int = 0; < restaurants.length(); i++){ jsonobject restaurant = restaurants.getjsonobject(i); if (restaurant != null){ string tempname = restaurant.getstring("name"); log.d(tag, "restaurant name: " + tempname); } } } } catch (jsonexception e) { log.d(tag, "exception catched: " + e); e.printstacktrace(); } }
here json file.
{ "results_found": 1281966, "results_start": 0, "results_shown": 2, "restaurants": [ { "restaurant": { "r": { "res_id": 16607974 }, "apikey": "123456789", "id": "16607974", "name": "bassine specialty cheese", "url": "https://www.zomato.com/bass-vic/bassine-specialty-cheese-bass?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1", "location": { "address": "2125 bass hwy", "locality": "bass", "city": "bass", "city_id": 1341, "latitude": "-38.4833750000", "longitude": "145.4670320000", "zipcode": "3995", "country_id": 14, "locality_verbose": "bass, bass" }, "switch_to_order_menu": 0, "cuisines": "ice cream", "average_cost_for_two": 7, "price_range": 1, "currency": "$", "offers": [], "thumb": "", "user_rating": { "aggregate_rating": "2.9", "rating_text": "average", "rating_color": "ffba00", "votes": "5" }, "photos_url": "https://www.zomato.com/bass-vic/bassine-specialty-cheese-bass/photos?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1#tabtop", "menu_url": "https://www.zomato.com/bass-vic/bassine-specialty-cheese-bass/menu?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1&openswipebox=menu&showminimal=1#tabtop", "featured_image": "", "has_online_delivery": 0, "is_delivering_now": 0, "deeplink": "zomato://restaurant/16607974", "has_table_booking": 0, "events_url": "https://www.zomato.com/bass-vic/bassine-specialty-cheese-bass/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1", "establishment_types": [] } }, { "restaurant": { "r": { "res_id": 17649424 }, "apikey": "3d93604b4a84d85f374f39ea3b644132", "id": "17649424", "name": "schobels' restaurant", "url": "https://www.zomato.com/columbus-tx/schobels-restaurant-columbus?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1", "location": { "address": "2020 milam st 78934", "locality": "columbus", "city": "columbus", "city_id": 9241, "latitude": "29.6965000000", "longitude": "-96.5405000000", "zipcode": "78934", "country_id": 216, "locality_verbose": "columbus, columbus" }, "switch_to_order_menu": 0, "cuisines": "german, southern", "average_cost_for_two": 25, "price_range": 2, "currency": "$", "offers": [], "thumb": "", "user_rating": { "aggregate_rating": "4.0", "rating_text": "very good", "rating_color": "5ba829", "votes": "164" }, "photos_url": "https://www.zomato.com/columbus-tx/schobels-restaurant-columbus/photos?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1#tabtop", "menu_url": "https://www.zomato.com/columbus-tx/schobels-restaurant-columbus/menu?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1&openswipebox=menu&showminimal=1#tabtop", "featured_image": "", "has_online_delivery": 0, "is_delivering_now": 0, "deeplink": "zomato://restaurant/17649424", "has_table_booking": 0, "events_url": "https://www.zomato.com/columbus-tx/schobels-restaurant-columbus/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1", "establishment_types": [] } } ] }
error getting: org.json.jsonexception: no value name
your jsonquery
contain jsonobject
inside jsonobject
named restaurant
need jsonobject jsonobject restaurant = restaurants.getjsonobject(i);
like jsonobject restaurantsjsonobject =restaurant.getjsonobject("restaurant");
try below example name object
try { jsonobject jsonobject = new jsonobject(jsonquery); if (jsonobject != null){ jsonarray restaurants = jsonobject.getjsonarray("restaurants"); (int = 0; < restaurants.length(); i++){ jsonobject restaurant = restaurants.getjsonobject(i); jsonobject restaurantsjsonobject =restaurant.getjsonobject("restaurant"); if(restaurantsjsonobject.has("name")) { string tempname = restaurantsjsonobject.getstring("name"); log.d(tag, "restaurant name: " + tempname); } } } } catch (jsonexception e) { log.d(tag, "exception catched: " + e); e.printstacktrace(); }
Comments
Post a Comment