通过 GSON 将 Twitter 公共时间线 (JSON) 解析为 Java 对象

发布于 2024-10-04 22:55:32 字数 2136 浏览 3 评论 0原文

我见过很多人在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

¢蛋碎的人ぎ生 2024-10-11 22:55:32

为什么要重新发明轮子?您可以使用 twitter4j。它完成所有解析,并为您提供所需的对象。

Why reinvent the wheel? You can use twitter4j. It does all the parsing, and gives you the needed objects.

你げ笑在眉眼 2024-10-11 22:55:32

所有字段都不是字符串。
例如:通知是布尔值

  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;


 }

并且

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;

}

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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文