美文网首页
Shell简单解析JSON

Shell简单解析JSON

作者: WeeverLu | 来源:发表于2022-08-21 00:14 被阅读0次

https://blog.csdn.net/zzhouqianq/article/details/78646861

使用awk解析JSON

使用

json='{"country":"China", "address":{"province":"guangdong","city":"shenzhen","numberOfPeople":17682000}, "numberOfPeople":"One billion four hundred million"}'

country=`getJsonValuesByAwk "$json" "country" "default"`
address=`getJsonValuesByAwk "$json" "address" ""`
province=`getJsonValuesByAwk "$address" "province" ""`
city=`getJsonValuesByAwk "$address" "city" ""`
numberOfPeople=`getJsonValuesByAwk "$json" "numberOfPeople" "a lot of"`

echo $country #"China"
echo $address #{"province":"guangdong","city":"shenzhen","numberOfPeople":17682000}
echo $province #"guangdong"
echo $city #"shenzhen"
echo $numberOfPeople #17682000 "One billion four hundred million"

原方法如下

### 方法简要说明:
### 1. 是先查找一个字符串:带双引号的key。如果没找到,则直接返回defaultValue。
### 2. 查找最近的冒号,找到后认为值的部分开始了,直到在层数上等于0时找到这3个字符:,}]。
### 3. 如果有多个同名key,则依次全部打印(不论层级,只按出现顺序)
### @author lux feary
###
### 3 params: json, key, defaultValue
function getJsonValuesByAwk() {
    awk -v json="$1" -v key="$2" -v defaultValue="$3" 'BEGIN{
        foundKeyCount = 0
        while (length(json) > 0) {
            # pos = index(json, "\""key"\""); ## 这行更快一些,但是如果有value是字符串,且刚好与要查找的key相同,会被误认为是key而导致值获取错误
            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)
        }
    }'
}

使用第三方jq

官网:https://stedolan.github.io/jq/
参考:https://cloud.tencent.com/developer/article/1393873
参考:https://www.jianshu.com/p/6de3cfdbdb0e

brew install jq

相关文章

  • Shell简单解析JSON

    https://blog.csdn.net/zzhouqianq/article/details/78646861...

  • shell解析json

    在shell脚本中有时候需要解析json字段信息,但是一般简单的sed&awk都存在问题,比如多层json结构解析...

  • shell解析json

    我要获取buildUrl地址, 还可以用jq https://stedolan.github.io/jq/[htt...

  • golang解析json数据

    golang官方包encoding/json包解析json数据太复杂,这里使用简单的json解析包simplejs...

  • Android之json解析

    最简单的json数组 2.解析方法

  • 理解JSON

    大纲:JSON概念JSON语法(简单值,对象,数组,结合)解析JSON 1、JSON是什么? JSON是一种数据格...

  • 15-Openwrt libubox shell上面的应用(JS

    很多情况下我们需要在shell下解析或者组合JSON数据,在libubox里面提供了一个操作json脚本在/usr...

  • Retrofit 2.0 笔记

    简单的用法 解析 JSON 自定义解析方法 RxJava + Retrofit 参考:RetrofitAndroi...

  • Gson

    谷歌对于JSON解析提供了一个开源框架称为GSON,相比于Java的Json要简单的多 解析 反向操作

  • 用shll脚本让测试环境自动部署

    个人专题目录 ​ 1. 用shll脚本让测试环境自动部署 2. shell脚本去解析json数据

网友评论

      本文标题:Shell简单解析JSON

      本文链接:https://www.haomeiwen.com/subject/atzmgrtx.html