通过 GSON 将 Twitter 公共时间线 (JSON) 解析为 Java 对象
我见过很多人在使用 GSON 库时遇到问题 - 我仍然找不到解决我的问题的方法。
我想要实现的是从 Twitter 的公共时间线 变成带有 GSON 的 POJO。
以下是我对 twitter 的 JSON 结构的表示:
public class OneTweet {
public OneTweet()
{
// Empty constructor
}
public String coordinates;
public String favorited;
public String created_at;
public String truncated;
public String text;
public String contributors;
public String id;
public String geo;
public String in_reply_to_user_id;
public String place;
public String in_reply_to_screen_name;
//public user user;
public List<user> user;
public String source;
public String something;
}
public class user {
public String name;
public String profile_sidebar_border_color;
public String profile_background_tile;
public String profile_sidebar_fill_color;
public String created_at;
public String profile_image_url;
public String location;
public String profile_link_color;
public String follow_request_sent;
public String url;
public String favourites_count;
public String contributors_enabled;
public String utc_offset;
public String id;
public String profile_use_background_image;
public String profile_text_color;
// protected is a reserved identifier
public String pprotected;
public String followers_count;
public String lang;
public String notifications;
public String time_zone;
public String verified;
public String profile_background_color;
public String geo_enabled;
public String description;
public String friends_count;
public String statuses_count;
public String profile_background_image_url;
public String following;
public String in_reply_to_status_id;
public String screen_name;
}
Map<String, OneTweet> json_string_array = googlejson.fromJson(jsonstring, new TypeToken<Map<String, OneTweet>>(){}.getType());
我当前收到以下错误:
线程“main”com.google.gson.JsonParseException 中出现异常:期望找到对象:false
我猜测我的POJO结构是错误的,或者可能与JSON对象“protected”是受保护的标识符有关爪哇。如果是后者,我不知道如何解决。
I've seen quite a few people having trouble with the GSON library - I still couldn't find a solution to my problem.
What I'm trying to achieve is to parse JSON from Twitter's public timeline into a POJO with GSON.
The following is my representation of twitter's JSON structure:
public class OneTweet {
public OneTweet()
{
// Empty constructor
}
public String coordinates;
public String favorited;
public String created_at;
public String truncated;
public String text;
public String contributors;
public String id;
public String geo;
public String in_reply_to_user_id;
public String place;
public String in_reply_to_screen_name;
//public user user;
public List<user> user;
public String source;
public String something;
}
public class user {
public String name;
public String profile_sidebar_border_color;
public String profile_background_tile;
public String profile_sidebar_fill_color;
public String created_at;
public String profile_image_url;
public String location;
public String profile_link_color;
public String follow_request_sent;
public String url;
public String favourites_count;
public String contributors_enabled;
public String utc_offset;
public String id;
public String profile_use_background_image;
public String profile_text_color;
// protected is a reserved identifier
public String pprotected;
public String followers_count;
public String lang;
public String notifications;
public String time_zone;
public String verified;
public String profile_background_color;
public String geo_enabled;
public String description;
public String friends_count;
public String statuses_count;
public String profile_background_image_url;
public String following;
public String in_reply_to_status_id;
public String screen_name;
}
Map<String, OneTweet> json_string_array = googlejson.fromJson(jsonstring, new TypeToken<Map<String, OneTweet>>(){}.getType());
I currently get the following error:
Exception in thread "main" com.google.gson.JsonParseException: Expecting object found: false
I'm guessing either my POJO structure is wrong or maybe it has to do with the fact that the JSON object "protected" is a protected identifier in Java. If the latter is the case I wouldn't know how to fix it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么要重新发明轮子?您可以使用 twitter4j。它完成所有解析,并为您提供所需的对象。
Why reinvent the wheel? You can use twitter4j. It does all the parsing, and gives you the needed objects.
所有字段都不是字符串。
例如:通知是布尔值
并且
all the fields are not String.
eg: notification is boolean
and