当前位置:首页 > 技能相关 > PowerBuilder > 正文内容

PB9:JSON处理对象的使用技巧

admin2年前 (2023-06-13)PowerBuilder7720 修订时间:2023-06-17 14:13:41

该篇内容主要介绍sailjson的使用方法,以及其内置函数的具体功能,不全面,仅实用的创建JSON、读取JSON内容、格式化JSON等。

具体代码附件:

sailjson.zip

首先我们要实例化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数组

上述功能函数具体的代码均在附件代码中,具体函数请附加形式添加。

 您阅读本篇文章共花了: 

免责声明
本站内容均为博客主本人日常使用记录的存档,如侵犯你的权益请联系:lifei@zaiheze.com 546262132@qq.com 沟通删除事宜。本站仅带访问端口形式使用,已杜绝搜索引擎爬取。

扫描二维码推送至手机访问。

版权声明:本文由LIFEI - blog发布,如需转载请注明出处。

本文链接:http://www.lifeiai.com/?id=306

分享给朋友:

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。