3

Something like this question

How to save in variables?

But with this json code:

{
 "restarutant": [
 {
 "name": "Hotel Raja",
 "photo": "http:\/\/i.imgur.com\/Mzt4u.jpg",
 "address": "93, 2ndc ross, GDP etx.",
 "area": "Vylaikaval",
 "city": "Bangalore",
 "rating": "4",
 "cuisines": {
 "first": "Chinese",
 "second": "Korean"
 }
 },
 {
 "name": "Hotel Raja2",
 "photo": "http:\/\/i.imgur2.com\/Mzt4u.jpg",
 "address": "93, 2ndc ross, GDP etx. number2",
 "area": "Vylaikaval2",
 "city": "Bangalore2",
 "rating": "4",
 "cuisines": {
 "first": "Chinese2",
 "second": "Korean2"
 }
 }
 ]
}

Code:

JSONObject json = new JSONObject(thepreviousjson);
JSONArray jArray = json.getJSONArray("restaurant");
String name[] = new String[jArray.length()];
String photo[] = new String[jArray.length()];
String address[] = new String[jArray.length()];
String area[] = new String[jArray.length()];
String city[] = new String[jArray.length()];
String rating[] = new String[jArray.length()];
String cuisines[] = new String[jArray.length()];
String first[] = new String[jArray.length()];
String second[] = new String[jArray.length()];
for(int i=0; i<jArray.length(); i++){
 JSONObject json_data = jArray.getJSONObject(i);
 name[i] = json_data.getString("name");
 photo[i] = json_data.getString("photo");
 address[i] = json_data.getString("address");
 area[i] = json_data.getString("area");
 city[i] = json_data.getString("city");
 rating[i] = json_data.getString("rating");
}

The point is to have stored: name[0] = "Hotel Raja"... name[1] = "Hotel Raja2" first[0] = Chinese, second[0] = Korean, first[1] = Chinese2, second[1] = Korean2

I have tried several combination, but nothing happens, what do I need to modify in my code? thanks

asked Nov 3, 2013 at 1:16

3 Answers 3

7

You can use List instead of String Array.

Better to use Model Concept.

Step:1)

Create a model for Restarutant.java

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

Step:2) Create Model for Cuisines

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

Final Step: Now how to store the data in Model after Parsing

List<Restarutant> list = new ArrayList<Restarutant>();
 try {
 JSONObject json = new JSONObject(thepreviousjson);
 JSONArray jArray = json.getJSONArray("restaurant");
 for (int i = 0; i < 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();
 }

Now how to retrieve value from List

for(int i=0;i<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);
 }

OutPut:

Name:Hotel Raja First:Chiense Second: Korean
Name:Hotel Raja2 First:Chiense2 Second: Korean2

Hope this will help you.

answered Nov 3, 2013 at 1:58
Sign up to request clarification or add additional context in comments.

1 Comment

this works for me, after editing a lot of code, I reached what I want, hope this help others in the future and thanks to you for you time.
1

you have to use ArrayList

for example

List<String> name = new ArrayList<String>();
for(int i=0; i<jArray.length(); i++){
JSONObject json_data = jArray.getJSONObject(i);
//by add new value to List the key will be the same key inside JSONarray 
name.add(json_data.getString("name"));
}
//and to call back value from List name
//get first element 0
String first_name = name.get(0);
answered Nov 3, 2013 at 1:36

1 Comment

but the "first" and "second" values, that two nested values are the problem.
1

Create a parcelable for your json response, see the code below:-

public class ObjectModel implements Parcelable{
 public String name;
 public String photo;
 public String address;
 public String area;
 public String city;
 public String rating;
 public HashMap<String,Object> cuisines;
 public ObjectModel(Parcel in)
 {
 name = in.readString();
 photo = in.readString();
 address = in.readString();
 area = in.readString();
 city = in.readString();
 rating = in.readString();
 in.readMap(cuisines, Object.class.getClassLoader());
 }
 @SuppressWarnings("rawtypes")
 public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
 @Override
 public ObjectModel createFromParcel(Parcel source) 
 {
 return new ObjectModel(source);
 }
 @Override
 public ObjectModel[] newArray(int size) 
 {
 return new ObjectModel[size];
 }
 }; 
 @Override
 public int describeContents() 
 {
 return 0;
 }
 @Override
 public void writeToParcel(Parcel dest, int flags) 
 {
 dest.writeString(name);
 dest.writeString(photo);
 dest.writeString(address);
 dest.writeString(area);
 dest.writeString(city);
 dest.writeString(rating);
 dest.writeMap(cuisines);
 }
 public ObjectModel()
 {
 }
}
//Write the parser 
private void parseJson(JSONObject baseJson) throws Exception
 {
 JSONArray jsonObjArray = baseJson.getJSONArray("restaurant");
 ObjectModel[] objectModelArray = new ObjectModel[jsonObjArray.length()];
 for(int index = 0;index<jsonObjArray.length();index++)
 {
 ObjectModel obj = new ObjectModel();
 HashMap<String,Object> data = new HashMap<String, Object>();
 JSONObject json = jsonObjArray.getJSONObject(index);
 obj.name = json.getString("name");
 obj.photo = json.getString("photo");
 obj.address = json.getString("address");
 obj.area = json.getString("area");
 obj.city = json.getString("city");
 obj.rating = json.getString("rating");// if it is a string 
 JSONObject cuis = json.getJSONObject("cuisines");
 data.put("first", cuis.getString("first"));
 data.put("second", cuis.getString("second"));
 obj.cuisines = data;
 objectModelArray[index] = obj;
 }
 }
answered Nov 3, 2013 at 2:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.