Answer the question
In order to leave comments, you need to log in
How to parse complex json using GSON library?
Parsing json returned by api methods vk.com wall.get https://vk.com/dev/wall.get?params[owner_id]=-1636...
Created a POJO using the site: www.jsonschema2pojo.org
Parent:
public class ResultObject extends ArrayList {
@SerializedName("response")
@Expose
private Response response;
public Response getResponse() {
return response;
}
public void setResponse(Response response) {
this.response = response;
}
}
Gson gson = new Gson();
ResultObject resultObject = gson.fromJson(result, ResultObject.class);
{
"response": {
"count": 1,
"items": [{
"id": 1,
"from_id": -163637512,
"owner_id": -163637512,
"date": 1521195983,
"marked_as_ads": 0,
"post_type": "post",
"text": "",
"attachments": [{
"type": "photo",
"photo": {
"id": 456239017,
"album_id": -6,
"owner_id": -163637512,
"user_id": 100,
"photo_75": "https://pp.userap...6c3/dpB1i0kmx28.jpg",
"photo_130": "https://pp.userap...6c4/chFSdx-5YcQ.jpg",
"photo_604": "https://pp.userap...6c5/fNDftqXiJek.jpg",
"width": 300,
"height": 500,
"text": "",
"date": 1521195983,
"post_id": 1
}
}],
"post_source": {
"type": "api",
"platform": "android",
"data": "profile_photo"
},
"comments": {
"count": 0,
"groups_can_post": true,
"can_post": 1
},
"likes": {
"count": 0,
"user_likes": 0,
"can_like": 1,
"can_publish": 1
},
"reposts": {
"count": 0,
"user_reposted": 0
},
"views": {
"count": 2
}
}],
"profiles": [],
"groups": [{
"id": 163637512,
"name": "ღThe Rivera Dynastyღ",
"screen_name": "ofrivera",
"is_closed": 0,
"type": "page",
"is_admin": 0,
"is_member": 0,
"photo_50": "https://pp.userap...6cd/MV1tb_1xmG0.jpg",
"photo_100": "https://pp.userap...6cc/e8swFwR2Pt0.jpg",
"photo_200": "https://pp.userap...6cb/iaKMY64vnt0.jpg"
}]
}
}
Answer the question
In order to leave comments, you need to log in
not at all the way you do it. look at the settings.
within the meaning of
{
"response": {...}
}
class Parrent {
Response response;
}
where is it difficult?
and why not use JSONObject???
example:
JSONObject result_json = new JSONObject(result)
if (result_json.has("response")) {
setResponse(data.get("response"));
}
JSONObject groups_json = new JSONObject(result_json .get("groups").toString());
Iterator<String> iter = groups_json.keys();
while (iter.hasNext()) {
String key = (String) iter.next();
String value = groups_json.get(key).toString();
key - имя элемента ("id","name","screen_name"...)
value - значение элемента (163637512, "ღThe Rivera Dynastyღ", "ofrivera"...)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question