【问题标题】:extract a field from twitter JSON string从 twitter JSON 字符串中提取一个字段
【发布时间】:2016-02-06 00:15:19
【问题描述】:
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);
}

【问题讨论】:

  • 你遇到了什么错误?

标签: java json parsing twitter


【解决方案1】:

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

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

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

你将能够解析它:

ObjectMapper objectMapper = new ObjectMapper();

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

【讨论】:

  • 嘿,谢谢大家,实际上我犯了一个非常愚蠢的错误......从命令行获取字符串。非常愚蠢,但感谢大家抽时间。
猜你喜欢
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-12
相关资源
最近更新 更多