-2

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"
    }
  ]
}
Mike
  • 1
  • 3
  • 2
    Possible duplicate of [How to parse JSON in Android](http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android) – Shank Aug 04 '16 at 17:14
  • i think u haven't read my question carefully.. I wanted to parse json using gson which is not duplicate question. Understand? – Mike Aug 06 '16 at 06:39

2 Answers2

0

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

Assume that your JSON is in String json

Create object of JSONObject and JSONArray to parse the JSON content.

JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray("posts");
for(int i = 0; i<jsonArray.length(); i++) {
    JSONObject ob = jsonArray.getJSONObject(i);
    String id = ob.getString("id");
    String fname = ob.getString("fname");
    String lname = ob.getString("lname");
}