• OpenWrt开发教程(五)shell命令操作json api详解(jshn.sh)


    摘自:https://blog.csdn.net/dxt1107/article/details/86587541

    json已经成为了通用的接口格式,各个语言中都有好用的json库,相信大家也都用得比较熟练。
    但在某些情况下我们需要在shell下操作json,如OpenWrt系统中在shell中封装ubus消息。
    OpenWrt shell操作json脚本在/usr/share/libubox/jshn.sh文件中,其实现是基于c语言的命令行工具。
    其基本思想是通过环境变量赋值最终组装成json串。
    下面介绍jshn.sh api接口的使用方法,并给出几个实例

    支持的数据类型

    1. string
    2. int
    3. boolean
    4. double

    api接口说明

    接口说明例子
    json_add_object 添加一个对象 json_add_object obj1
    json_add_string 添加string变量 json_add_string “name” “derry”
    json_add_int 添加一个对象 json_add_int “id” 111
    json_add_boolean 添加布尔变量 json_add_int “enable” 1
    json_add_array 添加数组 json_add_array “array”
    json_dump 显示当前的json串  
    json_select 切换到某个对象,有时候需要返回到上级对象操作 json_select dev-info
    json_get_var 获取某个变量值  
    json_init 初始化环境变量  

    读取json文件实例

    下面的例子用于读取一个json变量,并有json_select的使用方法

    json配置文件

    {
            "name":"Derry",
            "dev_info":{          
                    "name":"OpenWrt"
            }
    }
    
    

    读取以上配置文件shell脚本

    . /usr/share/libubox/jshn.sh
    json_load "$(cat /etc/derry/test.json)"
    #读取第一层的name, name=Derry
    json_get_var name1 name
    echo "name1 = $name1"
    #选择dev_info object
    json_select dev-info
    #读取dev_info.name
    json_get_var name2 name
    echo "name2 = $name2"

    运行结果如下:

    name1 = Derry
    name2 = OpenWrt
    
    

    封装json

    例子程序

    . /usr/share/libubox/jshn.sh
    json_init
    json_add_string "name" "Derry"
    json_add_int "id"  10
    json_add_boolean "enable" 1
    json_add_double "money" 10000000.08
    echo "cur=$JSON_CUR"
    json_add_object "class" 
    echo "cur=$JSON_CUR"
    echo "cur=$JSON_CUR"
    json_add_int "cls_id" 1001
    json_add_int "cls_name"  "class 1"
    echo "cur=$JSON_CUR"
    json_select ..
    echo "cur=$JSON_CUR"
    json_add_array  "array"
    json_add_string "" "value1"
    json_add_string "" "value2"
    echo "cur=$JSON_CUR"
    json_dump
    

    运行结果(可以根据结果找到以上对应代码照写即可)

    {
    	"name": "Derry",
    	"id": 10,
    	"enable": true,
    	"money": 10000000.080000,
    	"class": {
    		"cls_id": 1001,
    		"cls_name": 0
    	},
    	"array": ["value1", "value2"]
    }
    

    OpenWrt下读取uci文件并封装成json格式串

    uci配置文件:

    config appfilter appfilter
    	option enable 1
    	option appid_list "11 22 33 44 55 66"
    

    脚本文件:

    . /usr/share/libubox/jshn.sh
    . /lib/functions.sh
    json_init
    
    config_load appfilter
    config_get enable  "appfilter" enable
    config_get appid_list "appfilter" appid_list
    
    json_add_int "enable" $enable
    json_add_array "appid"
    for appid in $appid_list:
    do
    	echo "appid = $appid"
    	json_add_int "" $appid
    done
    
    json_str=`json_dump`
    echo "json=$json_str"
    

    运行结果

    appid = 11
    appid = 22
    appid = 33
    appid = 44
    appid = 55
    appid = 66:
    json={ "enable": 1, "appid": [ 11, 22, 33, 44, 55, 66 ] }
    

    以上为个人总结的OpenWrt shell json库使用方法,都是经过实测,如果对你有用,可以关注微信公众号 OpenWrt 获取更多OpenWrt开发技术文章,定期会发布OpenWrt相关干货

  • 相关阅读:
    初识Qt基于http协议网页浏览
    初识Qt涂鸦板绘制
    初识Qt图片显示、平移及旋转
    初识Qt文字绘制
    初识Qt鼠标、键盘事件及定时器和随机数
    初识Qt窗口界面
    初识Qt布局管理器
    解决VC++6.0打开文件或添加文件到工程出错的问题
    asp.net动态添加GridView的模板列,并获取列值
    asp.net中下载文件的问题
  • 原文地址:https://www.cnblogs.com/LiuYanYGZ/p/14212022.html
Copyright © 2020-2023  润新知