【问题标题】:Cannot Deserialise JSON to JAVA Object [closed]无法将 JSON 反序列化为 JAVA 对象 [关闭]
【发布时间】: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


【解决方案1】:

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

请注意,JSON object 用 { } 大括号划分,而 JSON array 以 [ ] 括号开始和结束(其中包含零个或多个对象,以逗号分隔)。当您告诉 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);

【讨论】:

  • 这很有帮助,谢谢!我不知道 JSON 对象有花括号。这是因为我的 JSON 以 [] 开头的问题。然后我可能应该寻找一种方法来告诉它寻找 JSON 数组而不是 Object。
  • @user2233834 不客气,看看编辑:)
猜你喜欢
  • 2021-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-08
  • 2014-08-10
  • 1970-01-01
  • 1970-01-01
  • 2018-10-03
相关资源
最近更新 更多