将从 API 收到的响应数据导入到 CSV 文件中

发布于 2025-01-17 07:06:42 字数 1958 浏览 1 评论 0原文

我正在尝试使用 Rest Assured 将从 API 响应中获取的数据存储到 CSV 文件中。 这就是我的代码的样子:

public void Test_01() throws Exception {
    Response response = RestAssured.given() // enter valid credentials
            .get("https://gorest.co.in/public-api/users")
            .then()
            .extract()
            .response();

    String data = response.jsonPath().get("data");

    System.out.println(response.asString().getClass());
    try
    {
        JsonNode jsonTree = new ObjectMapper().readTree(data);
        CsvSchema.Builder csvSchemaBuilder = CsvSchema.builder();
        JsonNode firstObject = jsonTree.elements().next();
        firstObject.fieldNames().forEachRemaining(fieldName -> {csvSchemaBuilder.addColumn(fieldName);} );
        CsvSchema csvSchema = csvSchemaBuilder.build().withHeader();
        CsvMapper csvMapper = new CsvMapper();
        csvMapper.writerFor(JsonNode.class)
                .with(csvSchema)
                .writeValue(new File("src/main/resources/lmsget.csv"), jsonTree);

    }   catch(Exception ex)

    {
        throw ex;
    }
}

当我尝试执行此测试时,我收到错误: Schema 指定要写入标题行;但不包含列名。也许,这是因为 JSON 的格式,它看起来像:

"code": 200,
"meta": {
"pagination": {
"total": 2610,
"pages": 131,
"page": 1,
"limit": 20
}

之后,是一个包含对象的数组 data 。我只想存储该数据。 我还尝试了将响应提取为 JsonPath 的一些更改:

Object data = response.JsonPath.get("data");

但是当我在 JsonNode jsonTree = new ObjectMapper().readTree(data); 中使用它时,我收到错误:

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('i' (code 105)): was expecting double-quote to start field name
at [Source: (String)"[{id=2583, name=Aryan Shah, [email protected], gender=male, status=inactive},

I'm trying to store a data taken from the API response into CSV file using Rest Assured.
That's how my code looks like:

public void Test_01() throws Exception {
    Response response = RestAssured.given() // enter valid credentials
            .get("https://gorest.co.in/public-api/users")
            .then()
            .extract()
            .response();

    String data = response.jsonPath().get("data");

    System.out.println(response.asString().getClass());
    try
    {
        JsonNode jsonTree = new ObjectMapper().readTree(data);
        CsvSchema.Builder csvSchemaBuilder = CsvSchema.builder();
        JsonNode firstObject = jsonTree.elements().next();
        firstObject.fieldNames().forEachRemaining(fieldName -> {csvSchemaBuilder.addColumn(fieldName);} );
        CsvSchema csvSchema = csvSchemaBuilder.build().withHeader();
        CsvMapper csvMapper = new CsvMapper();
        csvMapper.writerFor(JsonNode.class)
                .with(csvSchema)
                .writeValue(new File("src/main/resources/lmsget.csv"), jsonTree);

    }   catch(Exception ex)

    {
        throw ex;
    }
}

When I try to execute this test, I am getting an error: Schema specified that header line is to be written; but contains no column names. Maybe, that's because of the format of the JSON, which looks like:

"code": 200,
"meta": {
"pagination": {
"total": 2610,
"pages": 131,
"page": 1,
"limit": 20
}

After that, comes an array data with objects in it. I just want to store that data only.
I've also tried some changes in extracting response as JsonPath:

Object data = response.JsonPath.get("data");

But when I use it in JsonNode jsonTree = new ObjectMapper().readTree(data); I am getting error:

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('i' (code 105)): was expecting double-quote to start field name
at [Source: (String)"[{id=2583, name=Aryan Shah, [email protected], gender=male, status=inactive},

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舞袖。长 2025-01-24 07:06:42

解决方案:

  • 步骤 1:将响应转换为字符串格式的 json
  • 步骤 2:由 Jackson 提取数组 data,而不是 Rest-Assured
String res = given()
                .get("https://gorest.co.in/public-api/users")
                .asString();
try {
     JsonNode jsonTree = new ObjectMapper().readTree(res).get("data");
     ...
}  

Solution:

  • Step 1: Convert response to json in String format
  • Step 2: Extract array data by Jackson, not Rest-Assured
String res = given()
                .get("https://gorest.co.in/public-api/users")
                .asString();
try {
     JsonNode jsonTree = new ObjectMapper().readTree(res).get("data");
     ...
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文