AU3简易json读取示例

admin AutoIt3评论669字数 1721阅读模式

函数很简单,目的仅为处理post返回的json字符,此时用这个函数感觉要比正则方便一点了把

;先来一个简单的json数据格式
Local $sJson = '{"Name":"aopo","Gender":"BOY","Age":"22"}'
Local $sRead = _JsonRead($sJson, 'Gender')
MsgBox(0,0,$sRead)

;成员包括数组,解析方法如下
Local $sJson = '{"Name":"xiaoqiang","Gender":"BOY","Age":"22","Birthday":["1990","06","06"]}'
Local $sRead = _JsonRead($sJson, 'Birthday[2]');日
MsgBox(0,0,$sRead)

;成员的数组里包含一个或多个标准json数据
Local $sJson = '{"Name":"xiaoqiang","Gender":"BOY","Age":"22","Birthday":[{"Year":"1990","Month":"06","Day":"06"}]}'
Local $sRead = _JsonRead($sJson, 'Birthday[0].Month');月
MsgBox(0,0,$sRead)

;还是来点实战把,类似下面的字符相信大家分析网页时常遇到把?
;\u6807\u51C6json\u6570\u636E\u89E3\u6790\uFF0C\u4F60\u51C6\u5907\u597D\u4E86\u5417\uFF1F 
;下面是抓取WebQQ接收消息的数据包,也是标准的json格式,下面我们尝试用函数来解析下:
Local $sJson = '{"retcode":0,"result":[{"poll_type":"message","value":{"msg_id":4989,"from_uin":1329660231,"to_uin":1393052988,"msg_id2":375774,"msg_type":9,"reply_ip":176882144,"time":1335604922,"content":[["font",{"size":12,"color":"808040","style":[0,0,0],"name":"\u5B8B\u4F53"}],"\u6807\u51C6json\u6570\u636E\u89E3\u6790\uFF0C\u4F60\u51C6\u5907\u597D\u4E86\u5417\uFF1F "]}}]}'
Local $sRead = _JsonRead($sJson, 'result[0].value.content[1]');取出被编码的中文的源字符串
MsgBox(0,0,$sRead)

Func _JsonRead($sJson = "", $sMember = '')
        Local $sResult, $sCode = ""
        $sJson = StringRegExpReplace ($sJson, '[\r\n]+', '');去除换行
        $sCode &= 'function json2str()' & @CRLF
        $sCode &= '{' & @CRLF
        $sCode &= "var jsontext = '" & $sJson & "';"  & @CRLF
        $sCode &= 'var obj = eval("("+jsontext+")");' & @CRLF ; json2object
        $sCode &= 'var member = obj.' & $sMember & @CRLF ;取出信息
        $sCode &= 'return member' & @CRLF
        $sCode &= '}' & @CRLF
        ;ConsoleWrite($sCode)
        Local $oScript = ObjCreate("MSScriptControl.ScriptControl.1")
        $oScript.language = 'JavaScript'
        $oScript.AddCode($sCode)
        $sResult = $oScript.Run('json2str')
        $oScript = 0
        Return $sResult
EndFunc

版权声明:文章图片资源来源于网络,如有侵权,请留言删除!!!
广告也精彩
admin
  • 本文由 发表于 2023年7月8日 17:49:47
  • 转载请务必保留本文链接:https://www.58pxe.com/10710.html
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: