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

6080阅读 1评论2014-01-21 xu752526028
分类:网络与安全

1、JSON命令:
1
2
3
4
    
$ wget (32-bit system)
$ wget (64-bit system)
$ chmod +x ./jq
$ cp jq /usr/bin

2、JSON Schema:

json.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    
{
        "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:

1
2
    
$ cat json.txt | jq '.name'
"Google"

4、解析嵌套的JSON对象:

1
2
    
$ cat json.txt | jq '.location.city'
"Mountain View"

5、解析JSON数组:

1
2
3
4
5
    
$ cat json.txt | jq '.location | {street, city}'
{
  "city": "Mountain View",
  "street": "1600 Amphitheatre Parkway"
}
上一篇:兼容IE和Chrome的复制功能
下一篇:利用samba服务实现Linux与windows资源共享

文章评论