asp/vbscript解析json字符串生成json对象

  asp/vbscript将json字符解析为json对象的方法,如果asp使用jscript来编写服务器端代码操作json字符串就简单了,vbscript需要MSScriptControl.ScriptControl或者服务器端的jscript来作为中间体才行。

  vbscript将json字符解析为json对象的方法一,使用MSScriptControl.ScriptControl组件,请用IE浏览器运行本示例,会有安全提示,需要点击“是”允许创建。MSScriptControl.ScriptControl组件介绍

asp/vbscript解析json字符串生成json对象

<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)  
  
document.write(obj.a & "<br />") 
document.write(obj.b.name & "<br />")
document.write(obj.c.length & "<br />")
document.write(obj.c.get(0) & "<br />")
Set obj = Nothing 
</script>

来源:http://bbs.csdn.net/topics/390417254

评论(0)网络
阅读(84)喜欢(0)不喜欢(0)Asp/VBScript