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

1、JSON命令:

$ wget http://stedolan.github.io/jq/download/linux32/jq (32-bit system)
$ wget http://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"
}

你可能感兴趣的:(字符串,Google,System,download,country)