我正在制作一个web应用程序,返回Twitter追随者的Klout分数细节。工作流程如下:
在将JSON解析为Java时,我面临着问题。请提出解决办法。提前谢谢。
克洛
发布于 2017-06-17 14:28:35
看一看直接媒体技巧和技巧书中的直接媒体技巧和技巧部分。它解释了如何使用dmt-klout库来获取您要查找的信息。
如果要重写库,可以查看源代码。dmt库依赖于json.org类来解析JSON响应。例如:
public User(JSONObject json) {
nick = json.getString("nick");
id = new UserId(json.getString("kloutId"));
JSONObject scores = json.getJSONObject("score");
bucket = scores.getString("bucket");
score = scores.getDouble("score");
JSONObject scoreDeltas = json.getJSONObject("scoreDeltas");
dayChange = scoreDeltas.getDouble("dayChange");
weekChange = scoreDeltas.getDouble("weekChange");
monthChange = scoreDeltas.getDouble("monthChange");
}在本例中,json是使用查询用户时返回的String创建的JSONObject。此User类还用于影响查询:
public Influence(JSONObject json) {
parseInfluence(json.getJSONArray("myInfluencers"), myInfluencers);
parseInfluence(json.getJSONArray("myInfluencees"), myInfluencees);
}
private void parseInfluence(JSONArray array, List<User> list) {
int count = array.length();
for (int i = 0; i < count; i++) {
list.add(new User(
array.getJSONObject(i).getJSONObject("entity")
.getJSONObject("payload")));
}
}检索主题的方式略有不同:
public List<Topic> getTopics(UserId id) throws IOException {
List<Topic> topics = new ArrayList<Topic>();
JSONArray array = new JSONArray(KloutRequests.sendRequest(String.format(
KloutRequests.TOPICS_FROM_KLOUT_ID, getUserId(id).getId(), apiKey)));
int n = array.length();
for (int i = 0; i < n; i++) {
topics.add(new Topic(array.getJSONObject(i)));
}
return topics;
}Topic类的构造函数如下所示:
public Topic(JSONObject json) {
id = json.getLong("id");
name = json.getString("name");
displayName = json.getString("displayName");
slug = json.getString("slug");
displayType = json.getString("displayType");
imageUrl = json.getString("imageUrl");
}https://stackoverflow.com/questions/7524094
复制相似问题