Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use <code>List</code> instead of String <code>Array</code>.</p> <p><em><strong>Better to use Model Concept.</em></strong></p> <p><strong>Step:1)</strong></p> <p><em><strong>Create a model for Restarutant.java</em></strong></p> <pre><code>public class Restarutant { private String name; private String photoUrl; private String address; private String area; private String city; private int rating; private Cuisines cuisines; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhotoUrl() { return photoUrl; } public void setPhotoUrl(String photoUrl) { this.photoUrl = photoUrl; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getArea() { return area; } public void setArea(String area) { this.area = area; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public int getRating() { return rating; } public void setRating(int rating) { this.rating = rating; } public Cuisines getCuisines() { return cuisines; } public void setCuisines(Cuisines cuisines) { this.cuisines = cuisines; } } </code></pre> <p><em><strong>Step:2) Create Model for Cuisines</em></strong></p> <pre><code>public class Cuisines { private String first; private String second; public String getFirst() { return first; } public void setFirst(String first) { this.first = first; } public String getSecond() { return second; } public void setSecond(String second) { this.second = second; } } </code></pre> <p><em><strong>Final Step: Now how to store the data in Model after Parsing</em></strong></p> <pre><code>List&lt;Restarutant&gt; list = new ArrayList&lt;Restarutant&gt;(); try { JSONObject json = new JSONObject(thepreviousjson); JSONArray jArray = json.getJSONArray("restaurant"); for (int i = 0; i &lt; jArray.length(); i++) { JSONObject json_data = jArray.getJSONObject(i); Restarutant data = new Restarutant();// Create Object Here data.setName(json_data.getString("name")); data.setPhotoUrl(json_data.getString("photo")); data.setAddress(json_data.getString("address")); data.setArea(json_data.getString("area")); data.setCity(json_data.getString("city")); JSONObject cuisines = json_data.getJSONObject("cuisines"); Cuisines cuisine = new Cuisines();// Create Object here cuisine.setFirst(cuisines.getString("first")); cuisine.setSecond(cuisines.getString("second")); data.setCuisines(cuisine);// setting the cuisine list.add(data);// Finally adding the model to List } } catch (JSONException e) { e.printStackTrace(); } </code></pre> <p><em><strong>Now how to retrieve value from List</em></strong></p> <pre><code>for(int i=0;i&lt;list.size();i++){ Restarutant restarutant=list.get(i); String name=restarutant.getName(); String first=restarutant.getCuisines().getFirst(); String second=restarutant.getCuisines().getSecond(); System.out.println("Name: "+name+"First "+first+"Second "+second); } </code></pre> <p><em><strong>OutPut:</em></strong></p> <pre><code>Name:Hotel Raja First:Chiense Second: Korean Name:Hotel Raja2 First:Chiense2 Second: Korean2 </code></pre> <p>Hope this will help you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload