2 ways VBScript parses a JSON string into a JSON object _vbs

Source: Internet
Author: User

Asp/vbscript to parse JSON characters into JSON objects, it's easy if ASP uses JScript to write server-side code to manipulate the JSON string. VBScript requires Msscriptcontrol.scriptcontrol or server-side JScript as an intermediate.

VBScript method of parsing JSON characters into JSON objects

Using the Msscriptcontrol.scriptcontrol component, run this example with IE, and there will be a security prompt, which you need to click "Yes" to allow creation.

Examples of usages of msscriptcontrol.scriptcontrol components

Msscriptcontrol.scriptcontrol component Properties, methods, Events Introduction

Copy Code code as follows:

<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 ' Hello! ';}, email:[' "ycplxl1314@163.com" "," "ycplxl1314@gmail.com" "], family:{parents:[" "Father" "," "Mother" "],tostring:function" ( {return "family member" ";}}}"
Dim Objtest
Call Initscriptcontrol ' Initialize Msscriptcontrol. ScriptControl components
Set objtest = getjsonobject (strtest) ' Creates a JSON object
' Object property action
MsgBox objtest.name& "-" &objtest.age
' Array operation
Getjsarrayitem email,objtest.email,0
MsgBox Email
' Methods of execution
MsgBox Objtest.hello ()
MsgBox ObjTest.family.toString ()
</script>

VBScript method of parsing JSON characters into JSON objects two

Using JScript as an intermediate

Copy Code code as follows:

<script language= "JavaScript" >//when running on the server side, increase runat= "Server" property
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.