AU3簡(jiǎn)易json讀取示例

admin AutoIt3評(píng)論669字?jǐn)?shù) 1721閱讀模式

函數(shù)很簡(jiǎn)單,目的僅為處理post返回的json字符,此時(shí)用這個(gè)函數(shù)感覺(jué)要比正則方便一點(diǎn)了把

;先來(lái)一個(gè)簡(jiǎn)單的json數(shù)據(jù)格式
Local $sJson = '{"Name":"aopo","Gender":"BOY","Age":"22"}'
Local $sRead = _JsonRead($sJson, 'Gender')
MsgBox(0,0,$sRead)

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

;成員的數(shù)組里包含一個(gè)或多個(gè)標(biāo)準(zhǔn)json數(shù)據(jù)
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)

;還是來(lái)點(diǎn)實(shí)戰(zhàn)把,類(lèi)似下面的字符相信大家分析網(wǎng)頁(yè)時(shí)常遇到把?
;\u6807\u51C6json\u6570\u636E\u89E3\u6790\uFF0C\u4F60\u51C6\u5907\u597D\u4E86\u5417\uFF1F 
;下面是抓取WebQQ接收消息的數(shù)據(jù)包,也是標(biāo)準(zhǔn)的json格式,下面我們嘗試用函數(shù)來(lái)解析下:
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

版權(quán)聲明:文章圖片資源來(lái)源于網(wǎng)絡(luò),如有侵權(quán),請(qǐng)留言刪除!!!
廣告也精彩
admin
  • 本文由 發(fā)表于 2023年7月8日 17:49:47
  • 轉(zhuǎn)載請(qǐng)務(wù)必保留本文鏈接:http://yudch.cn/10710.html
匿名

發(fā)表評(píng)論

匿名網(wǎng)友 填寫(xiě)信息

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