我有一个包含以下格式数据的大文件。 (这是我对Twitter的API之一进行的API调用的响应)。我想从中提取字段“followers_count”的值。通常,我会使用以下命令使用jq执行此操作:cat | jq -r'.followers_count'
但这包含特殊字符,因此jq无法处理它。有人可以通过告诉我如何在JSON中转换它(例如使用shell脚本)或者如何在不转换的情况下获取followers_count字段来提供帮助吗?如果这种格式有特定的名称,我很想知道它。 谢谢。
文件中的示例行:
b'[{“id”:2361407554,“id_str”:“2361407554”,“name”:“hakimo ait”,“screen_name”:“hakimo_ait”,“location”:“”,“description”:“ ”, “URL”:空, “实体”:{ “描述”:{ “网址”:[]}}, “受保护”:假 “FOLLOWERS_COUNT”:0 “FRIENDS_COUNT”:6, “listed_count”:0 ,“created_at”:“Sun Feb 23 19:08:04 +0000 2014”,“favourites_count”:0,“utc_offset”:null,“time_zone”:null,“geo_enabled”:false,“valid”:false,“ statuses_count“:1,”lang“:”fr“,”status“:{”created_at“:”Sun Feb 23 19:09:21 +0000 2014“,”id“:437665498961293312,”id_str“:”437665498961293312“, “文本”: “C罗”, “截短的”:假, “实体”:{ “#标签”:[] “符号”:[], “user_mentions”:[] “网址”:[]}, “source”:“\ u003ca href = \”https:\ / \ / mobile.twitter.com \“rel = \”nofollow \“\ u003eMobile Web(M2)\ u003c \ / a \ u003e”,“in_reply_to_status_id”:空, “in_reply_to_status_id_str”:空, “in_reply_to_user_id”:空, “in_reply_to_user_id_str”:空, “in_reply_to_screen_name”:空, “地理”:空, “坐标”:空, “地方”:空, “贡献者”:空, “is_quote_status”:假的, “retweet_count”:0,“F avorite_count “:0,” 收藏 “:假,” 转推 “:假” 郎 “:” ES “},” contributors_enabled “:假” is_translator “:假” is_translation_enabled “:假” profile_background_color “:” C0DEED “ ”profile_background_image_url“: ”HTTP:\ / \ / abs.twimg.com \ /影像\ /主题\ / THEME1 \ /bg.png“, ”profile_background_image_url_https“:” HTTPS:\ / \ / abs.twimg.com \ /影像\ /主题\ / THEME1 \ /bg.png”, “profile_background_tile”:假的, “profile_image_url”: “HTTP:\ / \ / abs.twimg.com \ /粘\ / default_profile_images \ /default_profile_normal.png” “profile_image_url_https”: “HTTPS:\ / \ / abs.twimg.com \ /粘\ / 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”:真实的, “has_extended_profile”:假的, “DEFAULT_PROFILE”:真实的, “default_profile_image”:真实的, “以下”:假的, “follow_request_sent”:假“的通知“:假” translator_type “:” 无“}]'
答案 0 :(得分:0)
这不是有效的JSON,如果你想从这个响应中获取某些部分,你可以将这个结果转储到文件中,然后迭代它并获得你想要获取的文本。 否则,如果响应将在JSON中,它将通过jq库轻松解析,您也可以将此记录转储到文件中,将其转换为json然后解析它! 有多种方式'grep,awk,sed'.....你可以去找它!
从开头删除'b,从底部删除',它将成为有效的JSON!
答案 1 :(得分:0)
我已经删除了b'从一开始就是'从底部 !看看它是一个有效的JSON,现在我们可以像这样轻松地使用它! 我正在用我的档案来做....
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"
}
]