当前位置:首页 » JavaScript技术

Linux 通过命令行解析JSON字符串

2014-01-21 14:03 本站整理 浏览(18)
1、JSON命令:
$ wgethttp://stedolan.github.io/jq/download/linux32/jq (32-bit system)
$ wgethttp://stedolan.github.io/jq/download/linux64/jq (64-bit system)
$ chmod +x ./jq
$ cp jq /usr/bin
2、JSON Schema:
json.txt
{
        "name": "Google",
        "location":
                {
                        "street": "1600 Amphitheatre Parkway",
                        "city": "Mountain View",
                        "state": "California",
                        "country": "US"
                },
        "employees":
                [
                        {
                                "name": "Michael",
                                "division": "Engineering"
                        },
                        {
                                "name": "Laura",
                                "division": "HR"
                        },
                        {
                                "name": "Elise",
                                "division": "Marketing"
                        }
                ]
}
3、解析JSON object:
$ cat json.txt | jq '.name'
"Google"
4、解析嵌套的JSON对象:
$ cat json.txt | jq '.location.city'
"Mountain View"
5、解析JSON数组:
$ cat json.txt | jq '.location | {street, city}'
{
  "city": "Mountain View",
  "street": "1600 Amphitheatre Parkway"
}
本文出自 “Art_Hero” 博客,请务必保留此出处http://curran.blog.51cto.com/2788306/1353409