openwrt jshn tool and jshn.sh script analysis

I. Introduction

        jshn is a small tool for parsing or generating data in JSON format, and its source code is located in jshn.c of libubox. jshn.sh is an encapsulated script of jshn, located under /usr/share/libubox/ of the openwrt system. The jshn.sh script is mainly called by the openwrt script to generate and parse json. The interface between the shn tool and the jshn.sh script is introduced below.

2. jshn.sh script analysis

        Next, a script is used to explain the usage of some functions of the jshn.sh script. The script first builds a content as "{ "msg": "Hello, world!", "test": { "testdata": 1 }, "lan": [ "192.168.1.1", "192.168.10.1", "192.168 .100.1" ] }" json string, and then parse each member in it.

case:

#!/bin/sh
 
# source jshn shell library
. /usr/share/libubox/jshn.sh
 
# generating json data
json_init
json_add_string "msg" "Hello, world!"
json_add_object "test"
json_add_int "testdata" "1"
json_close_object
json_add_array lan
json_add_string 1 "192.168.1.1"
json_add_string 2 "192.168.10.1"
json_add_string 3 "192.168.100.1"
json_close_array
MSG=`json_dump`
# MSG now contains: { "msg": "Hello, world!", "test": { "testdata": 1 }, "lan": [ "192.168.1.1", "192.168.10.1", "192.168.100.1" ] }
 
 
# parsing json data
json_load "$MSG"
json_select test
json_get_var var1 testdata
json_select ..
json_get_var var2 msg
echo "msg: $var2 - testdata: $var1"

json_select lan
idx=1
while json_is_a $idx string
do
  json_get_var item $idx
  echo $item
  idx=$((idx+1))
done

. /usr/share/libubox/jshn.sh

        The interface that references the jshn.sh script, where jshn.sh is expanded.

json_init

        Initialize a json instance.

json_add_string "msg" "Hello, world!"

        Add a string json object, "msg": "Hello, world".

json_add_object "test"

        Add a test json object.

json_add_int "testdata" "1"

        Add an integer object to the test json object, "testdata": 1.

json_close_object

        Close the test object. After adding an object with json_add_object, you must call json_close_object to close the object later.

json_add_array lan

        Add an array of json objects, named lan.

json_add_string 1 "192.168.1.1"

json_add_string 2 "192.168.10.1"
json_add_string 3 "192.168.100.1"

        Items 1, 2, and 3 of the added array are "192.168.1.1", "192.168.10.1" and "192.168.100.1" respectively.

MSG=`json_dump`

        json_dump will print the json data added above as a string, which means that the printed content is assigned to the MSG variable. The content is { "msg": "Hello, world!", "test": { "testdata": 1 }, "lan": [ "192.168.1.1", "192.168.10.1", "192.168.100.1" ] } .

json_load "$MSG"

        json_load is used to parse json strings into json objects.

json_select test

        Select the test json object. Note: If the json object is nested in multiple layers, json_select can only select one layer at a time. If you want to get the innermost layer, json_select must operate multiple times .

json_get_var var1 test data

        json_get_var is used to get the value of testdata and save it in var1.        

json_select ..

        json_select selects the upper layer object.

json_get_var var2 msg

        json_get_var is used to get the value of msg and save it in var2.

json_select lan

        Select lan array json object.

idx=1

        The initialization index number is 1, and idx represents the subscript of the array.

while json_is_a $idx string

       json_is_a is used to determine the type of a json variable, here it is used to determine whether the idx item of the lan array json object is an array type.

json_get_var item $idx

echo $item

        Get the value of the idx item of the array json object, assign it to the item variable, and output it.

idx=$((idx+1))

        The value of idx is incremented by 1.

3. Summary

        This article uses a script case to explain the usage of some functions of jshn.sh. First, build a json object by adding functions, and then obtain the value of each json object through the analysis function, so as to deepen the familiarity with jshn.sh functions.

Guess you like

Origin blog.csdn.net/to_be_better_wen/article/details/130854679