简体   繁体   中英

How to retrieve value in the response of restassured

I got a response as below by using rest-assured:

{
    "data": {
        "hasMore": true,
        "galleries": [
            {
                "pic": "/xianfu/34b9a6d07bc91a659156d79da1d82f7d18432.jpg",
                "picInZoom": "/xianfu/34b9a6d07bc91a659156d79da1d82f7d18432.jpg@200w"
            },
            {
                "pic": "/xianfu/ca0b796b7e0f67cc9f78c73334a6404d26624.jpg",
                "picInZoom": "/xianfu/ca0b796b7e0f67cc9f78c73334a6404d26624.jpg@200w"
            },
            {
                "pic": "/xianfu/30da40b9c1eff6154989fc3c78c8292d43008.jpg",
                "picInZoom": "/xianfu/30da40b9c1eff6154989fc3c78c8292d43008.jpg@200w"
            },
            {
                "pic": "/xianfu/b59bda5cf828692c4af3a654f9b7309743008.jpg",
                "picInZoom": "/xianfu/b59bda5cf828692c4af3a654f9b7309743008.jpg@200w"
            },
            {
                "pic": "/xianfu/ac72069a937f03012532c32ae469a87e28672.jpg",
                "picInZoom": "/xianfu/ac72069a937f03012532c32ae469a87e28672.jpg@200w"
            },
            {
                "pic": "http://p0.baidu.com/xianfu/dcce14b596ad9d350cc2b64e039bf0a434816.jpg",
                "picInZoom": "http://p0.baidu.com/xianfu/dcce14b596ad9d350cc2b64e039bf0a434816.jpg@200w"
            },
            {
                "pic": "/xianfu/22fa3ac23efb0724202ac8fcaac01b5f16384.jpg",
                "picInZoom": "/xianfu/22fa3ac23efb0724202ac8fcaac01b5f16384.jpg@200w"
            },
            {
                "pic": "/xianfu/7b40420eef11b59af3aa0d1c0230cedc24576.jpg",
                "picInZoom": "/xianfu/7b40420eef11b59af3aa0d1c0230cedc24576.jpg@200w"
            }
        ]
    },
    "code": 0,
    "msg": "Success"
}

How can I get the value of first pic in response, with JAVA code, thanks a lot!

Try to convert the response into a string, with the toString() method

then transform that string into an object, like this:

JSONObject jsonObj = new JSONObject(homeAddress);

and from there, you can access it like this:

String pic = jsonObj.getJSONObject("data").getJSONArray("galleries").getJSONObject(0).getString("pic");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM