kover 发表于 2022-10-13 21:38

求助解析json

本帖最后由 kover 于 2022-10-14 11:42 编辑

curl下来的json数据是这样的
{"nums":5,"cityid":"101220103","city":"西宁","update_time":"2022-10-13 17:31:43","data":[{"date":"2022-10-13","wea":"晴转多云","wea_img":"yun","tem_day":"29","tem_night":"17","win":"东北风","win_speed":"<3级"},{"date":"2022-10-14","wea":"晴","wea_img":"qing","tem_day":"29","tem_night":"19","win":"东北风","win_speed":"<3级"},{"date":"2022-10-15","wea":"晴","wea_img":"qing","tem_day":"30","tem_night":"20","win":"东北风","win_speed":"<3级"},{"date":"2022-10-16","wea":"晴转多云","wea_img":"yun","tem_day":"31","tem_night":"20","win":"东北风","win_speed":"<3级"},{"date":"2022-10-17","wea":"晴转多云","wea_img":"yun","tem_day":"31","tem_night":"17","win":"东北风","win_speed":"<3级"},{"date":"2022-10-18","wea":"晴转多云","wea_img":"yun","tem_day":"28","tem_night":"15","win":"东北风","win_speed":"<3级"},{"date":"2022-10-19","wea":"晴","wea_img":"qing","tem_day":"28","tem_night":"15","win":"东北风","win_speed":"<3级"}]}
格式化后大概这样的
{"nums":3,
"cityid":"101220103",
"city":"西宁",
"update_time":"2022-10-13 16:05:29",
"data":[
{
"date":"2022-10-13",
"wea":"晴转多云",
"wea_img":"yun",
"tem_day":"29",
"tem_night":"17",
"win":"东北风",
"win_speed":"<3级"
},
{
"date":"2022-10-14",
"wea":"晴",
"wea_img":"qing",
"tem_day":"29",
"tem_night":"19",
"win":"东北风",
"win_speed":"<3级"
},
{
"date":"2022-10-15",
"wea":"晴",
"wea_img":"qing",
"tem_day":"30",
"tem_night":"20",
"win":"东北风",
"win_speed":"<3级"
},
{
"date":"2022-10-16",
"wea":"晴转多云",
"wea_img":"yun",
"tem_day":"31",
"tem_night":"20",
"win":"东北风",
"win_speed":"<3级"
},
{
"date":"2022-10-17",
"wea":"晴转多云",
"wea_img":"yun",
"tem_day":"31",
"tem_night":"17",
"win":"东北风",
"win_speed":"<3级"
},
{
"date":"2022-10-18",
"wea":"晴转多云",
"wea_img":"yun",
"tem_day":"28",
"tem_night":"15",
"win":"东北风",
"win_speed":"<3级"
},
{
"date":"2022-10-19",
"wea":"晴",
"wea_img":"qing",
"tem_day":"28",
"tem_night":"15",
"win":"东北风",
"win_speed":"<3级"
}]}

我想要这样的结果
近7天西宁天气
2022-10-13 晴转多云 17-29°C 东北风3级
2022-10-14 晴 19-29°C 东北风3级
....
2022-10-19 晴 15-28°C 东北风3级
结果是这样的
但不知道如何写?最好用shell写的,不想搞php或者python,那些需要环境太苛刻
大佬帮帮忙
按照3楼大佬的提示,用以下shell解决了
cat tq.txt|jq -r '.data[]'|awk-F ':' '{print$2}'|awk '{gsub(/["qing,yun<]/,"");print$0}'|awk '{printf "%s ", $0} END {printf "\n"}'|awk '{gsub(/级    /,"级\\n\\n");print$0}'|awk '{gsub(/    /,",");print$0}'|awk '{gsub(//,",");print$0}'|sed -n 's/,//p'|head
-c-2
新手写得不好,见笑了
结贴

0Ling0 发表于 2022-10-14 00:04

这个应该是你想要的https://blog.csdn.net/wxb880114/article/details/117445156,能够处理然后awk处理就OK

Takitooru 发表于 2022-10-14 00:16

本来想帮你,但是看到最后你不要php处理,只好帮你顶一下

平淡最真 发表于 2022-10-14 00:27

0Ling0 发表于 2022-10-14 00:04
这个应该是你想要的https://blog.csdn.net/wxb880114/article/details/117445156,能够处理然后awk处理就OK

写了个powershell的

$a='{"nums":5,"cityid":"101220103","city":"西宁","update_time":"2022-10-13 17:31:43","data":[{"date":"2022-10-13","wea":"晴转多云","wea_img":"yun","tem_day":"29","tem_night":"17","win":"东北风","win_speed":"<3级"},{"date":"2022-10-14","wea":"晴","wea_img":"qing","tem_day":"29","tem_night":"19","win":"东北风","win_speed":"<3级"},{"date":"2022-10-15","wea":"晴","wea_img":"qing","tem_day":"30","tem_night":"20","win":"东北风","win_speed":"<3级"},{"date":"2022-10-16","wea":"晴转多云","wea_img":"yun","tem_day":"31","tem_night":"20","win":"东北风","win_speed":"<3级"},{"date":"2022-10-17","wea":"晴转多云","wea_img":"yun","tem_day":"31","tem_night":"17","win":"东北风","win_speed":"<3级"},{"date":"2022-10-18","wea":"晴转多云","wea_img":"yun","tem_day":"28","tem_night":"15","win":"东北风","win_speed":"<3 级"},{"date":"2022-10-19","wea":"晴","wea_img":"qing","tem_day":"28","tem_night":"15","win":"东北风","win_speed":"<3级"}]}'
$b=$a |ConvertFrom-Json         
Write-Host ("近7天{0}天气" -f $b.city)                                                                                                
$b.data |%{Write-Host ("{0} {1} {3}-{2}度 {4}{5}" -f $_.date ,$_.wea,$_.tem_day,$_.tem_night,$_.win,$_.win_speed)}
read-host

kover 发表于 2022-10-14 08:58

平淡最真 发表于 2022-10-14 00:27
写了个powershell的

$a='{"nums":5,"cityid":"101220103","city":"西宁 ...

不错,win10下可以运行。有没有linux下的呢
页: [1]
查看完整版本: 求助解析json