【发布时间】:2021-07-09 19:37:25
【问题描述】:
我正在使用 RestAssured 自动化 twitter API
我正在尝试从 twitter API 获取响应:
String responseTweets = given().header("Authorization", "Bearer" +" "+token)
.queryParam("count", "5")
.queryParam("screen_name", "twitterapi")
.when().log().all()
.get("https://api.twitter.com/1.1/statuses/user_timeline.json").asString();
回复:
[
{
"created_at": "Wed Jun 30 17:30:46 +0000 2021",
"id": 1410289721977176000,
"id_str": "1410289721977176066",
"text": "Today, we’re launching new manage mutes into Twitter API v2.\n\n#TwitterAPI #v2 #EarlyAccess",
"truncated": false,
"entities": {
"hashtags": [
{
"text": "TwitterAPI",
"indices": [
62,
73
]
},
{
"text": "v2",
"indices": [
74,
77
]
},
{
"text": "EarlyAccess",
"indices": [
78,
90
]
}
],
"symbols": [],
"user_mentions": [],
"urls": [
{
"url": "",
"expanded_url": "https://twitter.com/TwitterDev/status/1410289221894651907",
"display_url": "twitter.com/TwitterDev/sta…",
"indices": [
91,
114
]
}
]
},
{
"created_at": "Tue Jun 29 20:44:15 +0000 2021",
"id": 1409976027426611200,
"id_str": "1409976027426611201",
"text": "Today, we announced the release of two new reliability features for the Twitter API v2 streaming endpoints: redunda… ",
"truncated": true,
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": [
{
"url": "",
"expanded_url": "",
"display_url": "twitter.com/i/web/status/1…",
"indices": [
117,
140
]
}
]
注意:响应太大,无法在此处打印。如果您注意到它以 Array 开头,我在帖子末尾添加了 treehirarchy img。请参考
当我试图运行下面的代码时。
JsonPath js1 = new JsonPath(responseTweets);
int len = js1.getInt("array.size()");
System.out.println("Length of the Array " + len);
System.out.println(js1.get("array[0].id"));
for(int i=0; i<len; i++)
{
System.out.println(js1.get("array["+i+"].text"));
}
它的显示输出:
Length of the Array 5
null
null
null
null
null
null
能否请您帮助我了解如何从这个 JSON 中获取值。
【问题讨论】:
标签: json api rest-assured rest-assured-jsonpath