V
V
vopross2018-03-16 18:29:31
Android
vopross, 2018-03-16 18:29:31

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

}

How to parse further and get data? Gives an error message:
Gson gson = new Gson();
ResultObject resultObject = gson.fromJson(result, ResultObject.class);

json structure:
{
"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"
}]
}
}

I've already broken my head how to parse this json

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2018-03-16
@red-barbarian

not at all the way you do it. look at the settings.
within the meaning of

{
"response": {...}
}

those. not Array
rather
class Parrent {
Response response;
}

M
Max Pleshkov, 2018-03-22
@zzWerOk

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

further, if you do not know the names of the elements, then:
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, "&#4326;The Rivera Dynasty&#4326;", "ofrivera"...)

    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question