如何反序列化对 List<Status> twitter4j 的 twitter 响应
How to deserialize twitter response to List<Status> twitter4j
我正在使用 rest-assured 和 twitter4j 来测试 twitter API。
所有调用都是通过 RestAssured 进行的,twitter4j 用于 Json 响应反序列化。
我想要做的是反序列化来自 twitter 的 Json 响应 - GET statuses/home_timeline
其中 returns 状态对象数组(来自 twitter4j)。
我可以像这里一样轻松反序列化一个 Status 对象:
@Test
public void verifyTwitCreation() {
RequestSpecification spec = new RqBuilder()
.withStatus(textToPublish)
.build();
Response response = twitClient.createTwit(spec);
assertResponseCode(response, 200);
String json = response.getBody().asString();
Status status = null;
try {
status = TwitterObjectFactory.createStatus(json);
} catch (TwitterException e) {
e.printStackTrace();
}
System.out.println(status.toString());
}
但我不知道如何对此类 Status 对象的数组进行反序列化。
我有点解决我的问题。我刚刚使用此插件从 Json 生成了 POJO - https://plugins.jetbrains.com/plugin/8634-robopojogenerator,然后使用放心
映射 Json
List<Status> statuses = Arrays.asList(response.as(Status[].class));
但我仍然会感谢使用 twitter4j 的解决方案的答案
尝试使用 JsonPath 提取状态列表,然后使用 TwitterObjectFactory
:
解析它们
Response response = twitClient.createTwit(spec);
List<Map<Object, Object>> responseList = response.jsonPath().getList("$");
ObjectMapper mapper = new ObjectMapper();
List<Status> statuses = responseList.stream().map(s -> {
Status status = null;
try {
String json = mapper.writeValueAsString(s)
status = TwitterObjectFactory.createStatus(json);
} catch (TwitterException | IOException e) {
e.printStackTrace();
}
return status;
}).collect(Collectors.toList());
您可以在解析期间将 try/catch 移动到一个单独的方法,这样它看起来更好:
public class TestClass {
@Test
public void verifyTwitCreation() {
RequestSpecification spec = new RqBuilder()
.withStatus(textToPublish)
.build();
Response response = twitClient.createTwit(spec);
List<Map<Object, Object>> responseList = response.jsonPath().getList("$");
List<Status> statuses = responseList.stream().map(TestClass::createStatus)
.collect(Collectors.toList());
}
private static Status createStatus(Map<Object, Object> jsonMap) {
Status status = null;
ObjectMapper mapper = new ObjectMapper();
try {
String json = mapper.writeValueAsString(jsonMap);
status = TwitterObjectFactory.createStatus(json);
} catch (TwitterException | IOException e) {
e.printStackTrace();
}
return status;
}
}
更新:
由于 JsonPath getList() returns 地图列表,我们应该将所有地图转换为 JSON 字符串,以便 TwitterObjectFactory 可以使用它。示例中使用了 Jackson 的 ObjectMapper,但可以使用任何 JSON 解析工具。
我正在使用 rest-assured 和 twitter4j 来测试 twitter API。
所有调用都是通过 RestAssured 进行的,twitter4j 用于 Json 响应反序列化。
我想要做的是反序列化来自 twitter 的 Json 响应 - GET statuses/home_timeline
其中 returns 状态对象数组(来自 twitter4j)。
我可以像这里一样轻松反序列化一个 Status 对象:
@Test
public void verifyTwitCreation() {
RequestSpecification spec = new RqBuilder()
.withStatus(textToPublish)
.build();
Response response = twitClient.createTwit(spec);
assertResponseCode(response, 200);
String json = response.getBody().asString();
Status status = null;
try {
status = TwitterObjectFactory.createStatus(json);
} catch (TwitterException e) {
e.printStackTrace();
}
System.out.println(status.toString());
}
但我不知道如何对此类 Status 对象的数组进行反序列化。
我有点解决我的问题。我刚刚使用此插件从 Json 生成了 POJO - https://plugins.jetbrains.com/plugin/8634-robopojogenerator,然后使用放心
映射 JsonList<Status> statuses = Arrays.asList(response.as(Status[].class));
但我仍然会感谢使用 twitter4j 的解决方案的答案
尝试使用 JsonPath 提取状态列表,然后使用 TwitterObjectFactory
:
Response response = twitClient.createTwit(spec);
List<Map<Object, Object>> responseList = response.jsonPath().getList("$");
ObjectMapper mapper = new ObjectMapper();
List<Status> statuses = responseList.stream().map(s -> {
Status status = null;
try {
String json = mapper.writeValueAsString(s)
status = TwitterObjectFactory.createStatus(json);
} catch (TwitterException | IOException e) {
e.printStackTrace();
}
return status;
}).collect(Collectors.toList());
您可以在解析期间将 try/catch 移动到一个单独的方法,这样它看起来更好:
public class TestClass {
@Test
public void verifyTwitCreation() {
RequestSpecification spec = new RqBuilder()
.withStatus(textToPublish)
.build();
Response response = twitClient.createTwit(spec);
List<Map<Object, Object>> responseList = response.jsonPath().getList("$");
List<Status> statuses = responseList.stream().map(TestClass::createStatus)
.collect(Collectors.toList());
}
private static Status createStatus(Map<Object, Object> jsonMap) {
Status status = null;
ObjectMapper mapper = new ObjectMapper();
try {
String json = mapper.writeValueAsString(jsonMap);
status = TwitterObjectFactory.createStatus(json);
} catch (TwitterException | IOException e) {
e.printStackTrace();
}
return status;
}
}
更新: 由于 JsonPath getList() returns 地图列表,我们应该将所有地图转换为 JSON 字符串,以便 TwitterObjectFactory 可以使用它。示例中使用了 Jackson 的 ObjectMapper,但可以使用任何 JSON 解析工具。