编程语言


VBScript把json字符串解析成json对象的2个方法

网络编程 VBScript把json字符串解析成json对象的2个方法 06-21

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

vbscript将json字符解析为json对象的方法一

使用MSScriptControl.ScriptControl组件,请用IE浏览器运行本示例,会有安全提示,需要点击“是”允许创建。

查看图片

MSScriptControl.ScriptControl组件的用法实例

MSScriptControl.ScriptControl组件属性、方法、事件介绍

<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)

使用vbscript生成36进制自动增长序号的实现代码
asp生成0~9,a~z的36进制字符串,运行下面示例需要使用IE核心的浏览器,其他非IE核心浏览器不支持vbscript。实现代码:scriptlanguage="vbscript"functiongetinitstrin

vbscript获取文件的创建时间、最后修改时间和最后访问时间的方法
setfso=createobject("Scripting.FileSystemObject")setfn=fso.GetFile("E:AD.txt")msgbox"文件创建时间:"&fn.DateCreatedmsgbox"文件最后修改时间:"&fn.DateLastModifiedmsgbox"文件最后访

vbs中获取脚本当前路径的2个方法
方法一:currentpath=createobject("Scripting.FileSystemObject").GetFolder(".").Path方法二:currentpath=createobject("Scripting.FileSystemObject").GetFile(Wscript.ScriptFullName).ParentFolder.Pa


编辑:编程语言

标签:方法,对象,时间,组件,文件