吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 919|回复: 4
收起左侧

[求助] 求助解析json

[复制链接]
kover 发表于 2022-10-13 21:38
本帖最后由 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

免费评分

参与人数 1热心值 +1 收起 理由
kover + 1 我再研究一下awk,多谢帮助

查看全部评分

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的

[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

免费评分

参与人数 1吾爱币 +1 收起 理由
kover + 1 用心讨论,共获提升!

查看全部评分

 楼主| kover 发表于 2022-10-14 08:58
平淡最真 发表于 2022-10-14 00:27
写了个powershell的

[mw_shl_code=powershell,true]$a='{"nums":5,"cityid":"101220103","city":"西宁 ...

不错,win10下可以运行。有没有linux下的呢
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 05:56

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表