将从 API 收到的响应数据导入到 CSV 文件中
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案:
data
,而不是 Rest-AssuredSolution:
data
by Jackson, not Rest-Assured