ehxz 发表于 2015-2-27 14:16:43

Pure Powerbuilder script tool to parse json【纯PB脚本工具解释JSON】


sailjson is a none visual user object for powerbuilder. This demo writted in Powerbuilder 9.You can free download sailjson and use it, but please keep the site address mark in script.
/*
Sailjson:from www.pblsoft.com
Please reserve this information
*/
sailjson provide follow functions to parse json data.
//the four function to parse json data and get values
public function string parse (string as_json)
public function integer getarray (string itemname, ref any va[])
public function boolean isarray (any value)
public function any getattribute (string itemname)
//this function to display json format in treeview
public subroutine buildtree (treeview atree, long handle, integer aobjectpcxidx, integer aarraypcxidx, integer aitempcxidx)
//the four function modify json data structure and get json data
public subroutine setattribute (string as_name, any aa_value)
public function any addarrayitem (string arrayname)
public function any addobject (string objname)
public function string getformatjson (string ident) //if ident='', will get packed json data
Please down load the demo to see details using.




Example Scripts示例json数据:

{
    "version": "1001",
    "header": {
      "count": 3,
      "comment": "items count"
    },
    "data": [
      {
            "colid": 1,
            "colname": "aaaaaa",
            "coladdr": ""
      },
      {
            "colid": 2,
            "colname": "bbbbbbbb",
            "coladdr": null
      },
      {
            "colid": 3,
            "colname": "cccccc"
      }
    ],
    "creattime": "20150213.084829"
}演示powerbuilder脚本解析json数据:
//this demo script parse the json data
sailjson json, ljson
string ls_json, ls
ls_json = '{"version":"1001","header":{"count":3,"comment":"itemscount"},"data":[{"colid":1,"colname":"aaaaaa","coladdr":""},{"colid":2,"colname":"bbbbbbbb","coladdr":null},{"colid":3,"colname":"cccccc"}],"creattime":"20150213.084829"}'

json = create sailjson

json.parse( ls_json )
//get version
ls = json.getattribute( 'version')
//get header, header is an object in json data
ljson = json.getattribute('header')
ls = string(ljson.getattribute('count'))
ls = ljson.getattribute('comment')

integer i,li_count
any larray[]

//get data, data is array of objects
li_count = json.getarray( 'data', larray)
for i = 1 to li_count
    ljson = larrayls = string(ljson.getattribute( 'colid'))
    ls = ljson.getattribute( 'colname')
    if isnull(ljson.getattribute( 'coladdr') ) then
      ls = 'null'
    else
      ls = ljson.getattribute( 'coladdr')
    end if
next

ls = json.getattribute( 'createtime')
//to display json format in treeview
integer handle
handle = tv_1.insertitemfirst(0, 'root', 2)
json.buildtree( tv_1, handle, 2,3,1)
tv_1.expanditem( handle)
destroy json


PB9代码,WINXP+PB9下测试通过




更新官方版本:


FROM:http://www.pblsoft.com/sailjson.htm

j528 发表于 2015-3-2 19:34:23

本帖最后由 j528 于 2015-6-5 00:12 编辑

{"emp":[{"id":111,"name":"\u5f20\u4e09"},{"id":123,"name":"\u674e\u56db"},{"id":124,"name":"\u738b\u4e94"},{"id":125,"name":"\u8d75\u516d"},{"id":126,"name":"zbao"}]
}

如果json数据是中文,如"name":"\u5f20\u4e09",怎样获取中文字符?


fuxiaoyang13 发表于 2015-3-4 15:01:17

学习学习!!!!

j528 发表于 2015-6-24 15:12:12

//====================================================================
oleobject json
oleobject wsh
wsh = CREATE oleobject
int li_ret
li_ret = wsh.ConnectToNewObject( "MSScriptControl.ScriptControl" )
if li_ret < 0 then
//messagebox
end if

string str
str='{"errcode": 0,"errmsg": "","retcode": 0,"recordlist" : [{ "openid" : "oDF3iY9WMaswOPWjCIp_f3Bnpljk","opercode" : 2002, "text" : " 您好,客服test1为您服务。","time" : 1400563710,"worker" : "test1"},{"openid" : "oDF3iY9WMaswOPWjCIp_f3Bnpljk","opercode" : 2003,"text" : "\u5f20\u4e09", "time" : 1400563731,"worker" : "test1"}]}'
wsh.language = "jscript"
wsh.addcode( " Array.prototype.get = function(idx){ return this; } ")
wsh.addcode( " Object.prototype.getattr = function(item){ return this; } ")
wsh.addcode( " Array.prototype.item = function(idx,item){ return this; } ")
json = wsh.Eval( '(' + str + ')' )


int tmp,li1
// 例如:注意要(tmp - 1),因为是从0 开始的
tmp=json.recordlist.length      //得出recordlist节点包含的数据行数
for li1=0 to tmp - 1
        messagebox("",string(json.recordlist.item(li1,"text")))
next
//即便text里面的字符为unicode的也能被转换为中文
destroy wsh
destroy json

j528 发表于 2015-6-24 15:12:57

这样就可以用了,可以把上面的代码直接贴到pb里面运行就知道怎样使用了

tomok 发表于 2015-8-23 16:32:50

谢谢 及时 提供

swimchen 发表于 2015-9-3 00:05:21

不错,这些都是好文章,还可以用INET连接某个网页来处理哦,现在网上有很多这种网页转换XML啦,不过前提得系统能够上网

qweasd2002 发表于 2015-12-17 14:19:43

这样就可以用了,可以把上面的代码直接贴到pb里面运行就知道怎样使用了
页: [1]
查看完整版本: Pure Powerbuilder script tool to parse json【纯PB脚本工具解释JSON】

免责声明:
本站所发布的一切破解补丁、注册机和注册信息及软件的解密分析文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:Admin@SybaseBbs.com