【问题标题】:How to get the values from JSON where JSON starts with Array Block in RestAssured如何从 JSON 中获取值,其中 JSON 以 RestAssured 中的 Array Block 开头
【发布时间】: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


    【解决方案1】:

    我使用 JsonPath (com.jayway.jsonpath.JsonPath)。与 JsonPath(io.restassured.path.json.JsonPath) 进行比较非常容易。

    List<String> list = JsonPath.read(res, "$[*].text");
    System.out.println(list);
    
    //["Today, we\u2019re launching new manage mutes into Twitter API v2.\n\n#TwitterAPI #v2 #EarlyAccess","Today, we announced the release of two new reliability features for the Twitter API v2 streaming endpoints: redunda\u2026 "]
    

    pom.xml

    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <version>2.6.0</version>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 2021-08-06
      • 2022-01-13
      • 2018-07-12
      • 1970-01-01
      相关资源
      最近更新 更多