如何在restassured的响应中检索值

时间:2017-10-30 12:53:59

标签: java rest-assured

我通过使用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"
}

如何通过JAVA代码获得第一张pic的值,非常感谢!

1 个答案:

答案 0 :(得分:1)

尝试使用toString()方法将响应转换为字符串

然后将该字符串转换为对象,如下所示:

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

从那里,您可以像这样访问它:

{{1}}