我正在制作一个Web应用程序,该应用程序返回Twitter关注者的Klout Scores详细信息。工作流程如下:
从Twitter API中,获取followers的twitter_id。例如:Sachin Tendulkar追随者的ID为48,000。
获取从步骤1接收到的twitter_id的twitter信息(屏幕名称,twitter_name,位置)。
从Klout API中,以JSON格式获取Klout分数,然后将JSON解析为Java。
从Klout API中,以JSON格式获取Klout主题,然后将JSON解析为Java。
将Klout和Twitter数据插入数据库。
我在将JSON解析为Java时遇到问题。请提出解决方案。
提前致谢 。
科马尔
最佳答案
看一下Setting up a Klout application书的Direct Media Tips and Tricks部分。它说明了如何使用dmt-klout库获取您要查找的信息。
如果要重写该库,可以查看源代码。 dmt-klout库依赖于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
是使用查询用户时返回的JSONObject
创建的String
。此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");
}
关于java - 从Klout API将JSON解析为Java,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7524094/