简体   繁体   中英

extract a field from twitter JSON string

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}}

Want to parse the above twitter JSON string to extract text field, The string validates as a valid JSON string when I check on online tools, But I am not able to parse the string using java library (Jackson). here is the code sample

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);
}

ObjectMapper should throws an org.codehaus.jackson.JsonParseException because your JSON string isn't valid. It musts begin with a { . You could use an online parser to check it.

If you start your JSON string like this :

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

You will be able to parse it :

ObjectMapper objectMapper = new ObjectMapper();

JsonNode jsonNode = objectMapper.readTree(jsonString);
jsonNode.get("text");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM