【发布时间】:2014-07-24 12:58:48
【问题描述】:
我正在尝试使用 Jackson 传递 JSON。但是我无法获得这些值,我不知道为什么。
这是我的结构:
JSON
[{"created_at":"Tue Jun 03 22:55:12 +0000 2014","id":473961123905937408,"id_str":"473961123905937408","text":"Bradley Wiggins omitted by Team Sky for Tour de France warm-up race http:\/\/t.co\/DHSvr1JPjm @Guardian_sport","source":"\u003ca href=\"http:\/\/www.hootsuite.com\" rel=\"nofollow\"\u003eHootsuite\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":87818409,"id_str":"87818409","name":"The Guardian","screen_name":"guardian","location":"London","description":"Top stories, special features, live blogs and more from http:\/\/t.co\/rrGq778cPt","url":"http:\/\/t.co\/rrGq778cPt","entities":{"url":{"urls":[{"url":"http:\/\/t.co\/rrGq778cPt","expanded_url":"http:\/\/theguardian.com","display_url":"theguardian.com","indices":[0,22]}]},"description":{"urls":[{"url":"http:\/\/t.co\/rrGq778cPt","expanded_url":"http:\/\/theguardian.com","display_url":"theguardian.com","indices":[56,78]}]}},"protected":false,"followers_count":2267310,"friends_count":1079,"listed_count":29928,"created_at":"Thu Nov 05 23:49:19 +0000 2009","favourites_count":130,"utc_offset":3600,"time_zone":"London","geo_enabled":false,"verified":true,"statuses_count":67775,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":true,"profile_background_color":"B2AFA9","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/704160749\/ff996aa3bc2009a2f9b97cdd43e8b5b7.png","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/704160749\/ff996aa3bc2009a2f9b97cdd43e8b5b7.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/2814613165\/f3c9e3989acac29769ce01b920f526bb_normal.png","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/2814613165\/f3c9e3989acac29769ce01b920f526bb_normal.png","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/87818409\/1352211339","profile_link_color":"005789","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"CAE3F3","profile_text_color":"333333","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":21,"favorite_count":4,"entities":{"hashtags":[],"symbols":[],"urls":[{"url":"http:\/\/t.co\/DHSvr1JPjm","expanded_url":"http:\/\/gu.com\/p\/3pmqn\/tw","display_url":"gu.com\/p\/3pmqn\/tw","indices":[68,90]}],"user_mentions":[{"screen_name":"guardian_sport","name":"Guardian sport","id":46403451,"id_str":"46403451","indices":[91,106]}]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"lang":"en"}]
推特流
private String created_at;
private int id;
private String id_str;
private String text;
//Getters and setters
//The following to print out what we get
@Override
public String toString() {
return "TwitterStream [created_at=" + getLang() + ", id=" + isPossibly_sensitive() + ", " +
"text=" + getFavorite_count() + "]";
}
检索TwitterFeeds
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
TwitterStream stream = mapper.readValue(new File("../TrustMetricsTwitter/files/JSONData.txt"), TwitterStream.class);
// or
List<TwitterStream> publisheDataList = mapper.convertValue(new FileReader("../TrustMetricsTwitter/files/JSONData.txt"), mapper.getTypeFactory().constructCollectionType(List.class, TwitterStream.class));
在运行时,两者都不会在 TwitterStream 类中打印出正确的 Java 对象作为 System.out.println。相反,TwitterStream 类为我要求它打印的值打印默认值。任何人都可以阐明一些观点吗?
例如
System.out.println(publisheDataList)
打印
[TwitterStream [created_at=null, id=false, text=0]]
和 System.out.println(流) 抛出
Can not deserialize instance of TwitterStream out of START_ARRAY token
【问题讨论】:
-
还有问题的 JSON?
-
除了 ID 太大而无法放入 int 并将被截断的实际问题(无论是什么)。
标签: java json object twitter jackson