asp/vbscript将json字符解析为json对象的方法,如果asp使用jscript来编写服务器端代码操作json字符串就简单了,vbscript需要MSScriptControl.ScriptControl或者服务器端的jscript来作为中间体才行。
vbscript将json字符解析为json对象的方法一
使用MSScriptControl.ScriptControl组件,请用IE浏览器运行本示例,会有安全提示,需要点击“是”允许创建。
MSScriptControl.ScriptControl组件的用法实例
MSScriptControl.ScriptControl组件属性、方法、事件介绍
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<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作为中间体
1
2
3
4
5
6
7
8
9
|
<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) </script> |
这篇文章就介绍到这了,需要的朋友可以参考一下。