0%

[OpenWrt]处理JSON

在openwrt开发过程中需要数据的发送和接受,此时传输数据的格式就有很多种了,今天简单介绍一下openwrt系统中JSON数据的处理方法

假设有一个JSON文件

1
2
3
4
5
6
7
8
9
10
11
12
cat /etc/ss.json
{
"server": "8.8.8.8",
"server_port": 443,
"local_address": "0.0.0.0",
"local_port": 7070,
"password": "fuckgfang",
"method": "aes_128_ctr",
"timeout": "60",
"protocol": "auth_aes128_sha1",
"fast_open": false
}

处理方式1

1
2
3
4
5
6
7
$ jsonfilter -i /etc/ss.json -e "@.server"
8.8.8.8

$ CONF=/etc/ss.json
$ a=$(jsonfilter -i $CONF -e "@.server")
$ echo $a
8.8.8.8

处理方式2
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
$ cat test.sh
source /usr/share/libubox/jshn.sh
json_init
data=$(cat /etc/ss.json)
json_load "$data"
json_get_var server_addr server
echo "server" $server_addr
echo "or......"
json_get_keys keys
for k in $keys; do
json_get_var v "$k"
echo "$k : $v"
done

$ sh test.sh
server 8.8.8.8
or......
server : 8.8.8.8
server_port : 443
local_address : 0.0.0.0
local_port : 7070
password : fuckfang
method : aes_128_ctr
timeout : 60
protocol : auth_aes128_sha1