无法将JSON反序列化为JAVA对象

时间:2014-06-04 02:09:46

标签: java json object twitter 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"}]

TwitterStream

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() + "]";
}

RetrieveTwitterFeeds

   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

1 个答案:

答案 0 :(得分:0)

如果没有看到您的JSON,我只能推测,但该消息似乎表明您正在尝试将JSON对象的数组反序列化为对象。你不能这样做。您只能将JSON数组反序列化为java对象的数组(或实现Collection的对象)。如果你考虑一下,如果你能......那就没有意义了。

请注意,JSON 对象{ }括号划分,而JSON 数组[ ]括号开头和结尾(在其中)是零个或多个对象,用逗号分隔)。当您告诉Jackson反序列化JSON对象时,它查找的第一个标记是{大括号(即START_OBJECT标记)。当它看到[括号(即START_ARRAY代币)时,它会退出并抱怨它。

在回复你的评论时,你可以“告诉它寻找一个JSON数组”,如:

 Foo[] deserialized = mapper.readValue(value, Foo[].class);

或者,在您的具体情况下,通过更改

TwitterStream stream = mapper.readValue(new File(...), TwitterStream.class);

TwitterStream[] stream = mapper.readValue(new File(...), TwitterStream[].class);