【问题标题】:How do I parse this supposedly JSON format如何解析这种所谓的 JSON 格式
【发布时间】: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


【解决方案1】:

这不是有效的 JSON,如果您想从该响应中获取某些特定部分,您可以将此结果转储到文件中,然后对其进行迭代并获取您想要获取的文本。 否则,如果响应是 JSON 格式,它会很容易通过 jq 库解析,您也可以将此记录转储到文件中,将其转换为 json 再解析! 'grep,awk,sed'有多种方法......你可以去!

去掉开头的'b和底部的',就变成有效的JSON了!

【讨论】:

  • 我已经尝试过了。从开头删除 c'[ 并在末尾删除单引号 - 由于文件中存在特殊字符,jq 无法解析输出,即使它在技术上可能是 JSON 表示。
【解决方案2】:

好吧,我从开头删除了 b',从底部删除了 '!看起来它是一个有效的 JSON,现在我们可以像这样轻松地使用 jq 了! 我正在处理我的文件....

jq -r '.accounts|keys[]' ../saadaccounts.json | while read key ; 
do
   DATA="$(jq ".accounts       [$key]" ../saadaccounts.json )"
   FNAME=$( echo $DATA  | jq -r '.first_name' )
   LNAME=$( echo $DATA  | jq -r '.Last_name'  )
done

*** YOUR JSON FILE *** 

[  
 {
  "id":2361393867,
  "id_str":"2361393867",
  "name":"graam a7bab",
  "screen_name":"bedoo691",
  "location":"",
  "description":"\u0627\u0633\u062a\u063a\u0641\u0631\u0627\u0644\u0644\u0647 \u0648\u0627\u062a\u0648\u0628 \u0627\u0644\u064a\u0647\u0647 ..!*",
  "url":null,
  "entities":{
     "description":{
        "urls":[

        ]
     }
  },
  "protected":false,
  "followers_count":1,
  "friends_count":6,
  "listed_count":0,
  "created_at":"Sun Feb 23 19:03:21 +0000 2014",
  "favourites_count":1,
  "utc_offset":null,
  "time_zone":null,
  "geo_enabled":false,
  "verified":false,
  "statuses_count":7,
  "lang":"ar",
  "status":{
     "created_at":"Tue Mar 04 16:07:44 +0000 2014",
     "id":440881284383256576,
     "id_str":"440881284383256576",
     "text":"@Naif8989",
     "truncated":false,
     "entities":{
        "hashtags":[

        ],
        "symbols":[

        ],
        "user_mentions":[
           {
              "screen_name":"Naif8989",
              "name":"\u200f naif alharbi",
              "id":540343286,
              "id_str":"540343286",
              "indices":[
                 0,
                 9
              ]
           }
        ],
        "urls":[

        ]
     },
     "source":"\u003ca href=\"http:\/\/twitter.com\/download\/android\" rel=\"nofollow\"\u003eTwitter for Android\u003c\/a\u003e",
     "in_reply_to_status_id":437675858485321728,
     "in_reply_to_status_id_str":"437675858485321728",
     "in_reply_to_user_id":2361393867,
     "in_reply_to_user_id_str":"2361393867",
     "in_reply_to_screen_name":"bedoo691",
     "geo":null,
     "coordinates":null,
     "place":null,
     "contributors":null,
     "is_quote_status":false,
     "retweet_count":0,
     "favorite_count":0,
     "favorited":false,
     "retweeted":false,
     "lang":"und"
  },
  "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:\/\/pbs.twimg.com\/profile_images\/437664693373911040\/ydODsIeh_normal.jpeg",
  "profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/437664693373911040\/ydODsIeh_normal.jpeg",
  "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":false,
  "following":false,
  "follow_request_sent":false,
  "notifications":false,
  "translator_type":"none"
  }
]

【讨论】:

  • 非常感谢您的帮助。我更改了样本以反映 jq 失败的输出中更棘手的部分。你能看看这个吗?
猜你喜欢
  • 2023-03-05
  • 2021-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-17
相关资源
最近更新 更多