网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的范文:

 

标题 VBScript把json字符串解析成json对象的2个方法
范文
    asp/vbscript将json字符解析为json对象的方法,如果asp使用jscript来编写服务器端代码操作json字符串就简单了,vbscript需要MSScriptControl.ScriptControl或者服务器端的jscript来作为中间体才行。
    vbscript将json字符解析为json对象的方法一
    使用MSScriptControl.ScriptControl组件,请用IE浏览器运行本示例,会有安全提示,需要点击“是”允许创建。
    名单
    代码如下:
    <script language="vbscript">
    Dim sc4Json
    Sub InitScriptControl
        Set sc4Json = CreateObject("MSScriptControl.ScriptControl")
        sc4Json.Language = "JavaScript"
        sc4Json.AddCode "var itemTemp=null;function getJSArray(arr, index){itemTemp=arr[index];}"
    End Sub
    Function getJSONObject(strJSON)
        sc4Json.AddCode "var jsonObject = " & strJSON
        Set getJSONObject = sc4Json.CodeObject.jsonObject
    End Function
    Sub getJSArrayItem(objDest,objJSArray,index)
        On Error Resume Next
        sc4Json.Run "getJSArray",objJSArray, index
        Set objDest = sc4Json.CodeObject.itemTemp
        If Err.number=0 Then Exit Sub
        objDest = sc4Json.CodeObject.itemTemp
    End Sub
    Dim strTest
    strTest = "{name:""alonely"", age:24,hello:function(){return '你好!';}, email:[""ycplxl1314@163.com"",""ycplxl1314@gmail.com""], family:{parents:[""父亲"",""母亲""],toString:function(){return ""家庭成员"";}}}"
    Dim objTest
    Call InitScriptControl'初始化MSScriptControl.ScriptControl组件
    Set objTest = getJSONObject(strTest)'创建JSON对象
    '对象属性操作
    msgbox objTest.name&"-"&objTest.age
    '数组操作
    getJSArrayItem email,objTest.email,0
    msgbox email
    '执行方法
    msgbox objTest.hello()
    msgbox objTest.family.toString()
    </script>
    vbscript将json字符解析为json对象的方法二
    用jscript作为中间体
    代码如下:
    <script language="javascript">//运行在服务器端时,增加runat="server"属性
    Array.prototype.get = function(x) { return this[x]; }; 
    function parseJSON(strJSON) { return eval("(" + strJSON + ")"); } 
    </script>
    <script language="vbscript">
    Dim json, obj 
    json = "{a:""aaa"", b:{ name:""bb"", value:""text"" }, c:[""item0"", ""item1"", ""item2""]}"
    Set obj = parseJSON(json) 
随便看

 

在线学习网范文大全提供好词好句、学习总结、工作总结、演讲稿等写作素材及范文模板,是学习及工作的有利工具。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/13 8:02:44