【发布时间】:2018-03-01 13:01:56
【问题描述】:
我有一个包含以下格式数据的大文件。 (这是我对 Twitter 的一个 API 进行的 API 调用的响应)。我想从中提取字段“followers_count”的值。通常,我会使用以下命令对 jq 执行此操作: cat | jq -r '.followers_count'
但这包含特殊字符,所以 jq 无法处理它。有人可以通过告诉我如何将其转换为 JSON(例如使用 shell 脚本)或如何在不进行转换的情况下获取 follower_count 字段来提供帮助吗?如果这种格式有一个特定的名称,我很想知道它。 谢谢。
文件中的示例行:
b'[{"id":2361407554,"id_str":"2361407554","name":"hakimo ait","screen_name":"hakimo_ait","location":"","description":" ","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":0,"friends_count":6,"listed_count":0 ,"created_at":"2014 年 2 月 23 日星期日 19:08:04 +0000","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false," statuses_count":1,"lang":"fr","status":{"created_at":"Sun Feb 23 19:09:21 +0000 2014","id":437665498961293312,"id_str":"437665498961293312", "text":"c.ronaldo","截断":false,"entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[]}, "来源":"\u003ca href=\"https:\/\/mobile.twitter.com\" rel=\"nofollow\"\u003eMobile Web (M2)\u003c\/a\u003e","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,"geo":null,"coordinates":null,"place":null,"contributors":null, "is_quote_status":false,"retweet_count":0,"f avorite_count":0,"favorited":false,"retweeted":false,"lang":"es"},"contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED ","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","profile_background_tile":false,"profile_image_url":"http:\/\/abs.twimg.com\/sticky\/default_profile_images\/default_profile_normal.png" ,"profile_image_url_https":"https:\/\/abs.twimg.com\/sticky\/default_profile_images\/default_profile_normal.png","profile_link_color":"1DA1F2","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color": "DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":true,"default_profile_image":true,"following":false,"follow_request_sent":false,"通知":false,"translator_type":"none"}]'
【问题讨论】:
-
你能去掉前两个
b'和最后一个'字符,然后通过JSON.parse运行值吗? -
看起来您将 Python 字符串的字符串表示形式转储到文件中,而不是 JSON 本身。第一步是将 JSON 正确转储到文件中。
-
@J.D.Pace:这个问题似乎与 JavaScript 无关,
-
@FelixKling:我看到“jq”并切换到“jquery”。
-
@J.D. Pace:试过了 - 没用!
标签: json