Mike Mike - 3 months ago 12
JSON Question

How can I parse following JSON response using GSON

This is JSON response from server, now how can I deserialize it.

{
"posts": [
{
"id": "1",
"fname": "yourFname1",
"lname": "yourLname1"
},
{
"id": "2",
"fname": "yourFname2",
"lname": "yourLname2"
},
{
"id": "111",
"fname": "star",
"lname": "trek"
},
{
"id": "111",
"fname": "star",
"lname": "trek"
}
]
}

Answer

first you need a javaBean

public class OuterBean {

    public ArrayList<InnerBean> posts;

    public class InnerBean{

        public String id;
        public String fname;
        public String lname;
    }

}

then parse the json call parseJson(name of json);

private void parseJson(String result) {
    Gson gson = new Gson();
    OuterBean outerBean = gson.fromJson(result, OuterBean.class);
}
Comments