Skip to content Skip to sidebar Skip to footer

Android Get Json Array Nested In Array

Something like this question How to save in variables? But with this json code: { 'restarutant': [ { 'name': 'Hotel Raja', 'photo': 'http:\/\/i

Solution 1:

You can use List instead of String Array.

Better to use Model Concept.

Step:1)

Create a model for Restarutant.java

publicclassRestarutant {
    privateString name;
    privateString photoUrl;
    privateString address;
    privateString area;
    privateString city;
    private int rating;
    privateCuisines cuisines;
    publicStringgetName() {
        return name;
    }
    publicvoidsetName(String name) {
        this.name = name;
    }
    publicStringgetPhotoUrl() {
        return photoUrl;
    }
    publicvoidsetPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl;
    }
    publicStringgetAddress() {
        return address;
    }
    publicvoidsetAddress(String address) {
        this.address = address;
    }
    publicStringgetArea() {
        return area;
    }
    publicvoidsetArea(String area) {
        this.area = area;
    }
    publicStringgetCity() {
        return city;
    }
    publicvoidsetCity(String city) {
        this.city = city;
    }
    public int getRating() {
        return rating;
    }
    publicvoidsetRating(int rating) {
        this.rating = rating;
    }
    publicCuisinesgetCuisines() {
        return cuisines;
    }
    publicvoidsetCuisines(Cuisines cuisines) {
        this.cuisines = cuisines;
    }

}

Step:2) Create Model for Cuisines

publicclassCuisines {
    privateString first;
    privateString second;
    publicStringgetFirst() {
        return first;
    }
    publicvoidsetFirst(String first) {
        this.first = first;
    }
    publicStringgetSecond() {
        return second;
    }
    publicvoidsetSecond(String second) {
        this.second = second;
    }
}

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

List<Restarutant> list = newArrayList<Restarutant>();
        try {
            JSONObject json = newJSONObject(thepreviousjson);
            JSONArray jArray = json.getJSONArray("restaurant");
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                Restarutant data = newRestarutant();// 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 = newCuisines();// 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.

Solution 2:

you have to use ArrayList

for example

List<String> name    = newArrayList<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 0String first_name = name.get(0);

Solution 3:

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

publicclassObjectModelimplementsParcelable{

    publicString name;
    publicString photo;
    publicString address;
    publicString area;
    publicString city;
    publicString rating;
    publicHashMap<String,Object> cuisines;


    publicObjectModel(Parcelin)
    {
       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")
    publicstatic final Parcelable.CreatorCREATOR = newParcelable.Creator() {

        @OverridepublicObjectModelcreateFromParcel(Parcel source) 
        {
            returnnewObjectModel(source);

        }

        @OverridepublicObjectModel[] newArray(int size) 
        {

            returnnewObjectModel[size];
        }
    }; 

    @Overridepublic int describeContents() 
    {
        return0;
    }

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

    publicObjectModel()
    {

    }

}


//Write the parser privatevoidparseJson(JSONObject baseJson) throws Exception
    {
        JSONArray jsonObjArray = baseJson.getJSONArray("restaurant");

        ObjectModel[] objectModelArray = newObjectModel[jsonObjArray.length()];
        for(int index = 0;index<jsonObjArray.length();index++)
        {
            ObjectModel obj = newObjectModel();
            HashMap<String,Object> data = newHashMap<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;

        }
    }

Post a Comment for "Android Get Json Array Nested In Array"