当前位置:网站首页>awk处理JSON处理
awk处理JSON处理
2022-07-07 18:37:00 【栗少】
嵌套解析和列表会有点 bug... 不过满足轻度使用了 #!/bin/bash json_str='{ "access_token":"12345678-1234-1234-1234-123456789012", "token_type":"bearer", "refresh_token":"12345678-1234-1234-1234-12345678901" }' get_json_value() { awk -v json="$1" -v key="$2" -v defaultValue="$3" 'BEGIN{ foundKeyCount = 0 while (length(json) > 0) { pos = match(json, "\""key"\"[ \\t]*?:[ \\t]*"); if (pos == 0) {if (foundKeyCount == 0) {print defaultValue;} exit 0;} ++foundKeyCount; start = 0; stop = 0; layer = 0; for (i = pos + length(key) + 1; i <= length(json); ++i) { lastChar = substr(json, i - 1, 1) currChar = substr(json, i, 1) if (start <= 0) { if (lastChar == ":") { start = currChar == " " ? i + 1: i; if (currChar == "{" || currChar == "[") { layer = 1; } } } else { if (currChar == "{" || currChar == "[") { ++layer; } if (currChar == "}" || currChar == "]") { --layer; } if ((currChar == "," || currChar == "}" || currChar == "]") && layer <= 0) { stop = currChar == "," ? i : i + 1 + layer; break; } } } if (start <= 0 || stop <= 0 || start > length(json) || stop > length(json) || start >= stop) { if (foundKeyCount == 0) {print defaultValue;} exit 0; } else { print substr(json, start, stop - start); } json = substr(json, stop + 1, length(json) - stop) } }' } echo $(get_json_value "${json_str}" "refresh_token")
边栏推荐
- Apifox 接口一体化管理新神器
- 【mysql篇-基础篇】事务
- 【论文阅读】MAPS: Multi-agent Reinforcement Learning-based Portfolio Management System
- 【函数递归】简单递归的5个经典例子,你都会吗?
- php 获取图片信息的方法
- 解决/bin/sh进去的容器运行可执行文件报not found的问题
- Is it safe to open a stock account at present? Can I open an account online directly.
- [MySQL - Basic] transactions
- 【网络原理的概念】
- I Basic concepts
猜你喜欢
How to test CIS chip?
Implement secondary index with Gaussian redis
Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
使用camunda做工作流设计,驳回操作
Dachang classic pointer written test questions
AIRIOT助力城市管廊工程,智慧物联守护城市生命线
如何满足医疗设备对安全性和保密性的双重需求?
I Basic concepts
复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算
写了个 Markdown 命令行小工具,希望能提高园友们发文的效率!
随机推荐
Implement secondary index with Gaussian redis
Introduction to referer and referer policy
Spark judges that DF is empty
Is it safe to open a stock account at present? Can I open an account online directly.
【论文阅读】MAPS: Multi-agent Reinforcement Learning-based Portfolio Management System
【C语言】指针进阶---指针你真的学懂了吗?
不落人后!简单好用的低代码开发,快速搭建智慧管理信息系统
Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
How to implement safety practice in software development stage
Alibaba cloud award winning experience: how to mount NAS file system through ECS
POJ 1742 Coins ( 单调队列解法 )「建议收藏」
【mysql篇-基础篇】事务
I Basic concepts
大厂经典指针笔试题
Phoenix JDBC
【奖励公示】第22期 2022年6月奖励名单公示:社区明星评选 | 新人奖 | 博客同步 | 推荐奖
写一下跳表
阿里云有奖体验:如何通过ECS挂载NAS文件系统
OneSpin 360 DV新版发布,刷新FPGA形式化验证功能体验
Try the tuiroom of Tencent cloud (there is an appointment in the evening, which will be continued...)