解析 json [推特流 API 1.1]

Parsing json [Twitter streaming API 1.1]

我能够从 Twitter 流 api 中读取 json 响应,并且 这是我必须解析的一条消息(推文):

JSON

{  
   "created_at":"Thu Apr 30 10:47:49 +0000 2015",
   "id":593728455901990912,
   "id_str":"593728455901990912",
   "text":"RT @yeahsjustin: when ur bieber af http:\/\/t.co\/dI7f9u6zNH",
   "source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\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":67267979,
      "id_str":"67267979",
      "name":"Glee Escarda",
      "screen_name":"ninjaglee",
      "location":"CGY, PH",
      "url":null,
      "description":"believe in yourself. because who else will?",
      "protected":false,
      "verified":false,
      "followers_count":602,
      "friends_count":309,
      "listed_count":4,
      "favourites_count":6413,
      "statuses_count":12548,
      "created_at":"Thu Aug 20 08:54:50 +0000 2009",
      "utc_offset":-25200,
      "time_zone":"Pacific Time (US & Canada)",
      "geo_enabled":false,
      "lang":"en",
      "contributors_enabled":false,
      "is_translator":false,
      "profile_background_color":"BADFCD",
      "profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme12\/bg.gif",
      "profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme12\/bg.gif",
      "profile_background_tile":false,
      "profile_link_color":"FF0000",
      "profile_sidebar_border_color":"F2E195",
      "profile_sidebar_fill_color":"FFF7CC",
      "profile_text_color":"0C3E53",
      "profile_use_background_image":true,
      "profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/593460442967576577\/L3sFiVWq_normal.jpg",
      "profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/593460442967576577\/L3sFiVWq_normal.jpg",
      "profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/67267979\/1409393209",
      "default_profile":false,
      "default_profile_image":false,
      "following":null,
      "follow_request_sent":null,
      "notifications":null
   },
   "geo":null,
   "coordinates":null,
   "place":null,
   "contributors":null,
   "retweeted_status":{  
      "created_at":"Thu Apr 30 10:12:47 +0000 2015",
      "id":593719640351637504,
      "id_str":"593719640351637504",
      "text":"when ur bieber af http:\/\/t.co\/dI7f9u6zNH",
      "source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\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":77120585,
         "id_str":"77120585",
         "name":"donna ",
         "screen_name":"yeahsjustin",
         "location":"melbourne | cliquesus ",
         "url":"https:\/\/twitter.com\/justinbieber\/status\/539319746403909632",
         "description":"justin drew bieber is all that matters.",
         "protected":false,
         "verified":false,
         "followers_count":24006,
         "friends_count":9937,
         "listed_count":390,
         "favourites_count":9775,
         "statuses_count":232120,
         "created_at":"Fri Sep 25 03:19:28 +0000 2009",
         "utc_offset":36000,
         "time_zone":"Melbourne",
         "geo_enabled":false,
         "lang":"en",
         "contributors_enabled":false,
         "is_translator":false,
         "profile_background_color":"FFFFFF",
         "profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/378800000169030556\/U4xBhhEf.png",
         "profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/378800000169030556\/U4xBhhEf.png",
         "profile_background_tile":false,
         "profile_link_color":"583F61",
         "profile_sidebar_border_color":"FFFFFF",
         "profile_sidebar_fill_color":"DDEEF6",
         "profile_text_color":"6A41B5",
         "profile_use_background_image":true,
         "profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/593723847972495360\/9gkMTMxw_normal.jpg",
         "profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/593723847972495360\/9gkMTMxw_normal.jpg",
         "profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/77120585\/1430389783",
         "default_profile":false,
         "default_profile_image":false,
         "following":null,
         "follow_request_sent":null,
         "notifications":null
      },
      "geo":null,
      "coordinates":null,
      "place":null,
      "contributors":null,
      "retweet_count":11,
      "favorite_count":10,
      "entities":{  
         "hashtags":[  ],
         "trends":[  ],
         "urls":[  ],
         "user_mentions":[  ],
         "symbols":[  ],
         "media":[  ]
      },
      "extended_entities":{  },
      "favorited":false,
      "retweeted":false,
      "possibly_sensitive":false,
      "filter_level":"low",
      "lang":"en"
   },
   "retweet_count":0,
   "favorite_count":0,
   "entities":{  
      "hashtags":[  ],
      "trends":[  ],
      "urls":[  ],
      "user_mentions":[  
         {  
            "screen_name":"yeahsjustin",
            "name":"donna ",
            "id":77120585,
            "id_str":"77120585",
            "indices":[  
               3,
               15
            ]
         }
      ],
      "symbols":[  ],
      "media":[  ]
   },
   "extended_entities":{  },
   "favorited":false,
   "retweeted":false,
   "possibly_sensitive":false,
   "filter_level":"low",
   "lang":"en",
   "timestamp_ms":"1430390869485"
}

我需要解析以下内容:

留言

作者

我使用 GSON 来解析消息,我的方法是这样的:

    Map map = gson.fromJson(message, Map.class);
    Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
    while (entries.hasNext()) {
        Map.Entry<Integer, Integer> entry = entries.next();
        System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
    }

这样我可以获得所有键值对并在两个不同的 POJO(作者和消息)中设置值。 我想知道我是否可以通过利用 GS​​ON 映射和反射功能来解析消息。 像这样:

 Author author = gson.fromJson(message, Author.class);
Message message= gson.fromJson(message, Message.class);

Author 和 Message classes 仅包含我们要解析的字段和一个函数 toString() 以字符串形式显示字段。

作者

public class Author {

    @SerializedName("id")
    private static long id;

    @SerializedName("created_at")
    private static String user_date;

    private static String screen_name;

    public  String toString() {
        return id + "" + user_date;         
    }

}

留言

public class Message {

    @SerializedName("user")
    Author author;   

    @SerializedName("created_at")
    String date;

    long id;
    String text;

    public String toString() {
        return id + "\n" + date + "\n" + text + "\n";       
    }

}

这样 class 消息的字段会被填充,而作者 class 结果会给出空值。

尽管如此,我不知道这是否是正确的方法。 有什么建议吗?

                             **SOLUTION**

从 Author 中删除 static 后一切正常..

看看这个USEFUL LINK

关于使用 Gson 功能将 JSON 映射到 Java 对象,您是正确的。创建一个 TwitterResponse class,其中将包含您感兴趣的字段:

class TwitterResponse {
    @SerializedName("user")
    Author author;
    String text;
    long id;
}

现在,Author 应该看起来像这样:

class Author {
    long id;
    String name;
    /* everything else you need */
}

并解析整个事情:

TwitterResponse response = gson.fromJson(message, TwitterResponse.class);