1月182017
AU3之简单的JSON读取解析
原出处在ACN论坛 by :komaau3
函数很简单,目的仅为处理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
另附JSMN:
;新加 $len 取对象长度
Func _JsonRead($sJson = "", $sMember = '', $len = 0)
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
If $len = 0 Then
$sCode &= 'var member = obj.' & $sMember & @CRLF ;取出信息
Else
$sCode &= 'var member = obj.' & $sMember & '.length' & @CRLF ;取出对象长度
EndIf
$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 ;==>;_JsonRead
扫描二维码,在手机上阅读
木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!
风哥牛逼
到35line提示错误。感觉没有编成基础想要自己做一个实现真难。