OpenWRT使用shell解析json

使用shell解析json依赖于/usr/share/libubox/jshn.sh文件

常用的获取信息的接口有

  • json_load
  • json_select
  • json_get_var

常用的构建json的接口有

  • json_init
  • json_add_object
  • json_add_string
  • json_add_int
  • json_add_boolean
  • json_add_array
  • json_dump

获取预设的LAN,WAN的接口名称

board.json文件为

root@Localhost:/www# cat /etc/board.json
{
     "switch": {
         "switch0": {
             "enable": true,
             "reset": true,
             "ports": [
                 {
                     "num": 5,
                     "device": "eth0",
                     "need_tag": false,
                     "want_untag": false
                 },
                 {
                     "num": 1,
                     "role": "lan",
                     "index": 1
                 },
                 {
                     "num": 2,
                     "role": "lan",
                     "index": 2
                 },
                 {
                     "num": 3,
                     "role": "lan",
                     "index": 3
                 },
                 {
                     "num": 4,
                     "role": "lan",
                     "index": 4
                 },
                 {
                     "num": 0,
                     "role": "wan"
                 }
             ],
             "roles": [
                 {
                     "role": "lan",
                     "ports": "1 2 3 4 5t",
                     "device": "eth0.1"
                 },
                 {
                     "role": "wan",
                     "ports": "0 5t",
                     "device": "eth0.2"
                 }
             ]
         }
     },
     "network": {
         "lan": {
             "ifname": "eth0.1",
             "protocol": "static"
         },
         "wan": {
             "ifname": "eth0.2",
             "protocol": "dhcp"
         }
     }
}

解析脚本如下

#!/bin/sh

. /lib/functions.sh

. /usr/share/libubox/jshn.sh

json_load “$(cat /etc/board.json)” 加载board描述文件

json_select network  切换network对象

json_select lan 切换到lan对象

json_get_var name ifname   从lan对象中获取ifname字段值保存在name中

echo “lan ifname is $name”

json_select ..   退回到上级对象

json_select wan 切换到wan对象

json_get_var name ifname 从wan对象中获取ifname字段值保存在name中

echo “wan ifname is $name”


脚本运行完毕后打印出

lan ifname is eth0.1

wan ifname is eth0.2

构建json

#!/bin.sh
. /usr/share/libubox/jshn.sh

json_init  初始化环境
json_add_object "test"  增加test对象
json_add_string "name" "test" 在test对象中增加name字段,值为test
json_add_int "id" 10086 在test对象中增加id字段,值为10086
json_add_boolean "online" 1 在test对象中增加online字段,值为true
json_add_double "money" 100.86 在test对象中增加money字段,值为100.86
echo "cur = $JSON_CUR" 打印当前对象名称
json_select .. 切换到上一层
json_add_array "array" 增加一个array数组
json_add_string "" "v1" 增加v1
json_add_string "" "v2" 增加v2
echo "cur = $JSON_CUR"
json_get_keys keys 遍历数组将其存放在keys
for k in $keys; do 遍历keys进行打印
         json_get_var v "$k"
         echo "$k -- $v"
done
echo "name is $name"
json_select .. 切换到上一层
echo "cur = $JSON_CUR"
json_add_int "gggg" 88888
json_add_string "gggg" "sdfsdfsdf"
json_dump

最终结果为:


cur = J_T1
cur = J_A2
1 -- v1
2 -- v2
name is
cur = J_V
{     "test": {
         "name": "test",
         "id": 10086,
         "online": true,
         "money": 100.86
         },
     "array": [ "v1", "v2" ],
     "gggg": "sdfsdfsdf"
}

加粗字体为对象的dump,我们看到gggg为int的8888并没有打印出来,也就是说在一个对象中字段名称不能重复

JSON和UCI

config timeserver 'ntp'
     option enabled '0'
     option enable_server '0'

脚本如下

#!/bin/sh
. /usr/share/libubox/jshn.sh
. /lib/functions.sh

json_init
config_load system
config_get __enable  "ntp" enable  获取ntp段中enable的值存放在__enable中

json_add_boolean enable $__enable
json_dump

运行结果

{ "enable": false }



posted on 2022-11-18 15:26  sudochen  阅读(153)  评论(0编辑  收藏  举报

导航