Parsing Twitter Public Timeline (JSON) into Java Objects via GSON
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开发者_如何学C 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.
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
import com.google.gson.annotations.SerializedName;
public class Tweets {
@SerializedName("geo")
public String geo;
@SerializedName("in_reply_to_status_id")
public String in_reply_to_status_id;
@SerializedName("truncated")
public String truncated;
@SerializedName("created_at")
public String created_at;
@SerializedName("retweet_count")
public String retweet_count;
@SerializedName("in_reply_to_user_id")
public String in_reply_to_user_id;
@SerializedName("id_str")
public String id_str;
@SerializedName("place")
public String place;
@SerializedName("favorited")
public boolean favorited;
@SerializedName("source")
public String source;
@SerializedName("in_reply_to_screen_name")
public String in_reply_to_screen_name;
@SerializedName("in_reply_to_status_id_str")
public String in_reply_to_status_id_str;
@SerializedName("id")
public long id;
@SerializedName("contributors")
public String contributors;
@SerializedName("coordinates")
public String coordinates;
@SerializedName("retweeted")
public boolean retweeted;
@SerializedName("text")
public String text;
@SerializedName("profile_image_url")
public String profile_image_url;
// public user user;
public User user;
}
and
import com.google.gson.annotations.SerializedName;
public class User {
@SerializedName("friends_count")
public int friends_count;
@SerializedName("profile_background_color")
public String profile_background_color;
@SerializedName("profile_background_image_url")
public String profile_background_image_url;
@SerializedName("created_at")
public String created_at;
@SerializedName("description")
public String description;
@SerializedName("favourites_count")
public int favourites_count;
@SerializedName("lang")
public String lang;
@SerializedName("notifications")
public boolean notifications;
@SerializedName("id_str")
public String id_str;
@SerializedName("default_profile_image")
public boolean default_profile_image;
@SerializedName("profile_text_color")
public String profile_text_color;
@SerializedName("default_profile")
public boolean default_profile;
@SerializedName("show_all_inline_media")
public boolean show_all_inline_media;
@SerializedName("contributors_enabled")
public boolean contributors_enabled;
@SerializedName("geo_enabled")
public boolean geo_enabled;
@SerializedName("screen_name")
public String screen_name;
@SerializedName("profile_sidebar_fill_color")
public String profile_sidebar_fill_color;
@SerializedName("profile_image_url")
public String profile_image_url;
@SerializedName("profile_background_tile")
public boolean profile_background_tile;
@SerializedName("follow_request_sent")
public boolean follow_request_sent;
@SerializedName("url")
public String url;
@SerializedName("statuses_count")
public int statuses_count;
@SerializedName("following")
public boolean following;
@SerializedName("time_zone")
public String time_zone;
@SerializedName("profile_link_color")
public String profile_link_color;
@SerializedName("protected")
public boolean protectedd;
@SerializedName("verified")
public boolean verified;
@SerializedName("profile_sidebar_border_color")
public String profile_sidebar_border_color;
@SerializedName("followers_count")
public int followers_count;
@SerializedName("location")
public String location;
@SerializedName("name")
public String name;
@SerializedName("is_translator")
public boolean is_translator;
@SerializedName("id")
public long id;
@SerializedName("listed_count")
public int listed_count;
@SerializedName("profile_use_background_image")
public boolean profile_use_background_image;
@SerializedName("utc_offset")
public int utc_offset;
}
精彩评论