从twitter JSON字符串中提取字段

时间:2015-11-05 08:37:20

标签: java json parsing twitter

Event:header:{timestamp=1446624314000}body: {"filter_level":"low","retweeted":false,"in_reply_to_screen_name":null,"possibly_sensitive":false,"truncated":false,"lang":"en","in_reply_to_status_id_str":null,"id":661816460197675008,"in_reply_to_user_id_str":null,"timestamp_ms":"1446624314723","in_reply_to_status_id":null,"created_at":"Wed Nov 04 08:05:14 +0000 2015","favorite_count":0,"place":null,"coordinates":null,"text":"Data Analytics in the football industry. Interview with former Real Madrid Digital Manager Topical https://t.co/zvM9kWocuD","contributors":null,"geo":null,"entities":{"symbols":[],"urls":[{"expanded_url":"http://ln.is/gettopical.com/bigda/NBl7k","indices":[99,122],"display_url":"ln.is/gettopical.com\u2026","url":"https://t.co/zvM9kWocuD"}],"hashtags":[],"user_mentions":[]},"is_quote_status":false,"source":"<a href=\"http://linkis.com\" rel=\"nofollow\">Linkis.com<\/a>","favorited":false,"in_reply_to_user_id":null,"retweet_count":0,"id_str":"661816460197675008","user":{"location":"http://aspgems.com","default_profile":true,"profile_background_tile":false,"statuses_count":2298,"lang":"en","profile_link_color":"0084B4","profile_banner_url":"https://pbs.twimg.com/profile_banners/259193124/1410273236","id":259193124,"following":null,"protected":false,"favourites_count":42,"profile_text_color":"333333","verified":false,"description":"Passionate about any technology that can improve the Digital Business and the online Education. Partner at ASPGEMS. Opinions are personal.","contributors_enabled":false,"profile_sidebar_border_color":"C0DEED","name":"JUANJO MARTINEZPAGAN","profile_background_color":"C0DEED","created_at":"Tue Mar 01 10:10:06 +0000 2011","default_profile_image":false,"followers_count":618,"profile_image_url_https":"https://pbs.twimg.com/profile_images/1258494534/JuanjoMart_nez_normal.JPG","geo_enabled":true,"profile_background_image_url":"http://abs.twimg.com/images/themes/theme1/bg.png","profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme1/bg.png","follow_request_sent":null,"url":"http://neurok.es","utc_offset":3600,"time_zone":"Madrid","notifications":null,"profile_use_background_image":true,"friends_count":899,"profile_sidebar_fill_color":"DDEEF6","screen_name":"JuanjoMartinezP","id_str":"259193124","profile_image_url":"http://pbs.twimg.com/profile_images/1258494534/JuanjoMart_nez_normal.JPG","listed_count":59,"is_translator":false}}

想要解析上面的twitter JSON字符串以提取文本字段,当我检查在线工具时,字符串验证为有效的JSON字符串,但我无法使用java库(Jackson)解析字符串。这是代码示例

private static JsonNode rootNode;

public static void main(String args[]) throws IOException {

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    //mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
    rootNode = mapper.readTree(new StringReader(args[0]));
    JsonNode innerNode = rootNode.get("text");
    //the customerSessionId has a String value
    String myString = innerNode.asText();

    System.out.println("customerSessionId is:" + myString);
}

1 个答案:

答案 0 :(得分:0)

88.198.255.218应抛出ObjectMapper,因为您的JSON字符串无效。它必须以org.codehaus.jackson.JsonParseException开头。您可以使用online parser进行检查。

如果你像这样开始你的JSON字符串:

{

您将能够解析它:

String jsonString = "{" +
    "\"filter_level\":\"low\"," +
    "\"retweeted\":false," +
    "\"in_reply_to_screen_name\":null," +
    "\"possibly_sensitive\":false," +
    "\"truncated\":false," +
    "\"lang\":\"en\"," +
   .......