PB9:JSON处理对象的使用技巧
该篇内容主要介绍sailjson的使用方法,以及其内置函数的具体功能,不全面,仅实用的创建JSON、读取JSON内容、格式化JSON等。
具体代码附件:
首先我们要实例化JSON对象,对于多层结构的,务必多实例化几个对象;
//实例化JSON处理对象 sailjson ljson,ljson1 //创建对象 ljson = create sailjson ... ... //结尾务必释放对象 destroy ljson
一、创建JSON
1.1创建JSON串,格式如下:
{ "version": "1001", "count": 3, "comment": "items count", "creattime": "20230613.223750" }
PB代码:
sailjson ljson, ljson1 ljson = create sailjson ljson.setattribute( 'version', '1001') ljson.setattribute( 'count', 3) ljson.setattribute( 'comment', 'items count') ljson.setattribute( 'creattime', string(now(),'yyyymmdd.hhmmss')) mle_1.text = ljson.getformatjson(' ') destroy ljson
上述代码使用了函数setattribute与getformatjson,函数方法具体功能如下:
//添加as_name节点,值为aa_value setattribute(as_name,aa_value) //如下使用,JSON会为无格式样式 getformatjson('') //格式化输出JSON,如上述代码样式 getformatjson(' ')
1.2 JSON串,格式如下:
{ "version": "1001", "header": { "count": 3, "comment": "items count" }, "creattime": "20230613.224654" }
PB代码:
sailjson ljson, ljson1 ljson = create sailjson ljson.setattribute( 'version', '1001') ljson1 = ljson.addobject( 'header') ljson1.setattribute( 'count', 3) ljson1.setattribute( 'comment', 'items count') ljson.setattribute( 'creattime', string(now(), 'yyyymmdd.hhmmss')) mle_1.text = ljson.getformatjson(' ') destroy ljson
上述代码又使用了函数addobject,函数方法具体功能如下:
//创建节点,并且赋值为新的ljson1串 //后续添加ljson1串的节点信息与内容即可实现上述样式JSON addobject(as_name)
1.3 JSON串,格式如下:
{ "version": "1001", "data": [ { "colid": 1, "colname": "aaaaaa", "coladdr": "" }, { "colid": 2, "colname": "bbbbbbbb", "coladdr": "" }, { "colid": 3, "colname": "cccccc" } ], "creattime": "20230613.225143" }
PB代码:
sailjson ljson, ljson1 ljson = create sailjson ljson.setattribute( 'version', '1001') ljson1 = ljson.addarrayitem( 'data') ljson1.setattribute( 'colid', 1) ljson1.setattribute( 'colname', 'aaaaaa') ljson1.setattribute( 'coladdr', '') ljson1 = ljson.addarrayitem( 'data') ljson1.setattribute( 'colid', 2) ljson1.setattribute( 'colname', 'bbbbbbbb') ljson1.setattribute( 'coladdr', '') ljson1 = ljson.addarrayitem( 'data') ljson1.setattribute( 'colid', 3) ljson1.setattribute( 'colname', 'cccccc') ljson.setattribute( 'creattime', string(now(), 'yyyymmdd.hhmmss')) mle_1.text = ljson.getformatjson(' ') destroy ljson
上述代码又使用了函数addarrayitem,函数方法具体功能如下:
//创建节点as_name,其格式为数组类型 //赋值的ljson1添加一组JSON串,再次创建同名节点,即可填入数组类型格式 //实现上述格式的展示 addarrayitem(as_name)
二、格式化JSON
2.1 getformatjson(样式)
该函数用来确定输出的json串是什么样式的,取决于样式是什么值,一般为: '', ' ',对应结果一串或者格式化显示
{ "version": "1001", "count": 3, "comment": "items count", "creattime": "20230613.223750" }
{"version": "1001","count": 3,"comment": "items count","creattime": "20230613.233227"}
三、读取JSON内容
原始JSON格式语句:
{ "version": "1001", "header": { "count": 3, "comment": "items count" }, "data": [ { "colid": 1, "colname": "aaaaaa", "coladdr": "" }, { "colid": 2, "colname": "bbbbbbbb", "coladdr": "" }, { "colid": 3, "colname": "cccccc" } ], "creattime": "20230614" }
识别数据区域:
version:1001 header: count:3 comment:items count data: item1 colid:1 colname:aaaaaa coladdr: item2 colid:2 colname:bbbbbbbb coladdr: item3 colid:3 colname:cccccc creattime:20230614
PB代码:
sailjson json, ljson json = create sailjson json.parse(mle_1.text) mle_2.clear() mle_2.text = 'version:' + json.getattribute('version') +'~r~n' mle_2.text += 'header:' +'~r~n' ljson = json.getattribute('header') mle_2.text += 'count:' +string(ljson.getattribute('count'))+'~r~n' mle_2.text += 'comment:' +string(ljson.getattribute('comment'))+'~r~n' integer i,li_count any larray[] mle_2.text += 'data:' +'~r~n' li_count = json.getarray('data',larray) for i = 1 to li_count ljson = larray[i] mle_2.text += 'item'+string(i) +'~r~n' mle_2.text += 'colid:'+ string(ljson.getattribute('colid')) +'~r~n' mle_2.text += 'colname:'+ string(ljson.getattribute('colname')) +'~r~n' if ljson.getattribute('coladdr') <> "null" then mle_2.text += 'coladdr:'+ string(ljson.getattribute('coladdr')) +'~r~n' end if next mle_2.text += 'creattime:' + string(json.getattribute('creattime')) +'~r~n' destroy json
上述代码又使用了函数parse(JSON)、getattribute(as_name)、getarray(as_name,any数组),函数方法具体功能如下:
//给JSON对象赋值JSON串 parse(JSON) //根据节点获取其值 getattribute(as_name) //根据一级节点,获取JSON组元素,存放在any数组中 //使用for循环遍历JSON组节点信息 getarray(as_name,any数组
上述功能函数具体的代码均在附件代码中,具体函数请附加形式添加。