Ansar
Ansar

Reputation: 364

Nested Json parsing using Gson

I have tried to parse this JSON using gson but I couldn’t. Can anyone help me to parse this JSON using gson?

JSON:

{
    "status": "ok",
    "results": {
        "favourites": [
            {
                "id": "UB3172",
                "name": "Masdar Headquarters"
            },
            {
                "id": "UB1438",
                "name": "Guggenheim Abu Dhabi on Saadiyat Island"
            },
            {
                "id": "UB4838",
                "name": "Watani Residential Development in Abu Dhabi - 600 Villas and 48 Buildings"
            },
            {
                "id": "UB4795",
                "name": "Two Mosques in Mohammed Bin Zayed City"
            },
            {
                "id": "UB1274",
                "name": "2 Workers Residential City at Al Ain Industrial City"
            }
        ]
    }
}

I tried this one for JSON parser class:

public class ProjectList {
    public String status;
    public String results;

    public class Favourites{
    public  String id;
    public  String name;
    }

}

In MainActivit

Reader reader = new InputStreamReader(result);
        Gson gson=new Gson();
        List<ProjectList.Favourites> fav=new ArrayList<ProjectList.Favourites>();
        fav=Arrays.asList(gson.fromJson(reader, ProjectList.Favourites.class));

Upvotes: 0

Views: 449

Answers (3)

savepopulation
savepopulation

Reputation: 11921

You can generate your pojos here : http://www.jsonschema2pojo.org/

Sometimes gson cannot convert your objects from json. In this case you have to write your own deserializer and use it with gson builder.

Edit: If you use proguard before release your project (if you set proguard to change your pojos variable names) gson cannot match class variable names, json names so it cannot convert your objects. You have to add @SerializedName("your_variable_name") annotaion.

Upvotes: 1

NullPointerException
NullPointerException

Reputation: 4008

Create a POJO class as follows

class MyResponse {
        public String status;

        public Result results;

        public static class Favourites {

            public String id;
            public String name;
        }

        public static class Result {

            public List<Favourites> favourites;
        }

    }

and pass it to gson as

MyResponse response = new Gson().fromJson(yourResponse, MyResponse.class);

idea is that maintain the hierarchy of key-value pairs with appropriate POJO's

Upvotes: 2

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

Try this way,hope this will help you to solve your problem.

String jsonRespone = "{\"status\":\"ok\",\"results\":{\"favourites\":[{\"id\":\"UB3172\",\"name\":\"Masdar Headquarters\"},{\"id\":\"UB1438\",\"name\":\"Guggenheim Abu Dhabi on Saadiyat Island\"},{\"id\":\"UB4838\",\"name\":\"Watani Residential Development in Abu Dhabi - 600 Villas and 48 Buildings\"},{\"id\":\"UB4795\",\"name\":\"Two Mosques in Mohammed Bin Zayed City\"},{\"id\":\"UB1274\",\"name\":\"2 Workers Residential City at Al Ain Industrial City\"}]}}";

String status;
ArrayList<HashMap<String,String>> favouritesList = new ArrayList<HashMap<String, String>>();

try{
   JSONObject responseJson = new JSONObject(jsonRespone);
   status = responseJson.getString("status");

   JSONArray favouriteJsonArray = responseJson.getJSONObject("results").getJSONArray("favourites");

   for (int i=0;i<favouriteJsonArray.length();i++){
       HashMap<String,String> favourite = new HashMap<String, String>();
       favourite.put("id",favouriteJsonArray.getJSONObject(i).getString("id"));
       favourite.put("name",favouriteJsonArray.getJSONObject(i).getString("name"));
       favouritesList.add(favourite);
   }

   System.out.print("status : "+status);
   for (HashMap<String, String> favourite : favouritesList) {
        System.out.print("id : "+favourite.get("id"));
        System.out.print("name : "+favourite.get("name"));
   }
}catch (Throwable e){
   e.printStackTrace();
}

Upvotes: 0

Related Questions