由于分析错误,VbsJson正在返回所需的对象
我是JSON新手,我正在尝试读取从数据源返回的JSON格式的字符串。我使用的是经典ASP,我从VbsJson的类代码中得到以下错误 Microsoft VBScript运行时错误“800a01a8” 所需对象 /MyTestPage.asp,第83行 在下面的代码中,我注释了发生错误的地方。我希望代码能够解析并提供要排序的键/值对数据 下面是JSON字符串由于分析错误,VbsJson正在返回所需的对象,json,vbscript,asp-classic,Json,Vbscript,Asp Classic,我是JSON新手,我正在尝试读取从数据源返回的JSON格式的字符串。我使用的是经典ASP,我从VbsJson的类代码中得到以下错误 Microsoft VBScript运行时错误“800a01a8” 所需对象 /MyTestPage.asp,第83行 在下面的代码中,我注释了发生错误的地方。我希望代码能够解析并提供要排序的键/值对数据 下面是JSON字符串 [ { "branchNumber": null, "createdOn": "2017-02-03T22:44:22.
[
{
"branchNumber": null,
"createdOn": "2017-02-03T22:44:22.656062",
"employeeId": "00",
"id": "0000000-000F-DB00-999D",
"lastUpdatedOn": "2017-02-04T17:26:37.137217",
"name": {
"firstName": "MyFirstName",
"lastName": "MyLastName",
"middleNamesOrInitial": null,
"preferredFirstName": null,
"prefix": null,
"suffix": null
},
"userName": "MyEMail@MyCorp.com"
},
{
"branchNumber": null,
"createdOn": "2017-02-03T22:44:22.656062",
"employeeId": "01",
"id": "0000000-000F-DB00-999F",
"lastUpdatedOn": "2017-02-04T17:26:37.137217",
"name": {
"firstName": "MyFirstName",
"lastName": "MyLastName",
"middleNamesOrInitial": null,
"preferredFirstName": null,
"prefix": null,
"suffix": null
},
"userName": "MyEMail2@MyCorp.com"
}
]
下面是我调用VbsJson的代码:
Dim simonResponseArray
Dim jsonClsUser, jsonParsedUser
Dim fso, json
Set json = New VbsJson
Set fso = server.CreateObject("Scripting.Filesystemobject")
simonXmlResponse = fso.OpenTextFile("C:\Temp\users_small.json").ReadAll
Set jsonClsUser = New VbsJson
Set jsonParsedUser = jsonClsUser.Decode(simonXmlResponse) 'This is where the error occurs
此外,下面是VbsJson的类代码:
Class VbsJson
'Author: Demon
'Date: 2012/5/3
'Website: http://demon.tw
Private Whitespace, NumberRegex, StringChunk
Private b, f, r, n, t
Private Sub Class_Initialize
Whitespace = " " & vbTab & vbCr & vbLf
b = ChrW(8)
f = vbFormFeed
r = vbCr
n = vbLf
t = vbTab
Set NumberRegex = New RegExp
NumberRegex.Pattern = "(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?"
NumberRegex.Global = False
NumberRegex.MultiLine = True
NumberRegex.IgnoreCase = True
Set StringChunk = New RegExp
StringChunk.Pattern = "([\s\S]*?)([""\\\x00-\x1f])"
StringChunk.Global = False
StringChunk.MultiLine = True
StringChunk.IgnoreCase = True
End Sub
'Return a JSON string representation of a VBScript data structure
'Supports the following objects and types
'+-------------------+---------------+
'| VBScript | JSON |
'+===================+===============+
'| Dictionary | object |
'+-------------------+---------------+
'| Array | array |
'+-------------------+---------------+
'| String | string |
'+-------------------+---------------+
'| Number | number |
'+-------------------+---------------+
'| True | true |
'+-------------------+---------------+
'| False | false |
'+-------------------+---------------+
'| Null | null |
'+-------------------+---------------+
Public Function Encode(ByRef obj)
Dim buf, i, c, g
Set buf = CreateObject("Scripting.Dictionary")
Select Case VarType(obj)
Case vbNull
buf.Add buf.Count, "null"
Case vbBoolean
If obj Then
buf.Add buf.Count, "true"
Else
buf.Add buf.Count, "false"
End If
Case vbInteger, vbLong, vbSingle, vbDouble
buf.Add buf.Count, obj
Case vbString
buf.Add buf.Count, """"
For i = 1 To Len(obj)
c = Mid(obj, i, 1)
Select Case c
Case """" buf.Add buf.Count, "\"""
Case "\" buf.Add buf.Count, "\\"
Case "/" buf.Add buf.Count, "/"
Case b buf.Add buf.Count, "\b"
Case f buf.Add buf.Count, "\f"
Case r buf.Add buf.Count, "\r"
Case n buf.Add buf.Count, "\n"
Case t buf.Add buf.Count, "\t"
Case Else
If AscW(c) >= 0 And AscW(c) <= 31 Then
c = Right("0" & Hex(AscW(c)), 2)
buf.Add buf.Count, "\u00" & c
Else
buf.Add buf.Count, c
End If
End Select
Next
buf.Add buf.Count, """"
Case vbArray + vbVariant
g = True
buf.Add buf.Count, "["
For Each i In obj
If g Then g = False Else buf.Add buf.Count, ","
buf.Add buf.Count, Encode(i)
Next
buf.Add buf.Count, "]"
Case vbObject
If TypeName(obj) = "Dictionary" Then
g = True
buf.Add buf.Count, "{"
For Each i In obj
If g Then g = False Else buf.Add buf.Count, ","
buf.Add buf.Count, """" & i & """" & ":" & Encode(obj(i))
Next
buf.Add buf.Count, "}"
Else
Err.Raise 8732,,"None dictionary object"
End If
Case Else
buf.Add buf.Count, """" & CStr(obj) & """"
End Select
Encode = Join(buf.Items, "")
End Function
'Return the VBScript representation of ``str(``
'Performs the following translations in decoding
'+---------------+-------------------+
'| JSON | VBScript |
'+===============+===================+
'| object | Dictionary |
'+---------------+-------------------+
'| array | Array |
'+---------------+-------------------+
'| string | String |
'+---------------+-------------------+
'| number | Double |
'+---------------+-------------------+
'| true | True |
'+---------------+-------------------+
'| false | False |
'+---------------+-------------------+
'| null | Null |
'+---------------+-------------------+
Public Function Decode(ByRef str)
Dim idx
str=Replace(str,"[]","[""""]")
str=Replace(str,"{}","[""""]")
idx = SkipWhitespace(str, 1)
If Mid(str, idx, 1) = "{" Then
Set Decode = ScanOnce(str, 1)
Else
Decode = ScanOnce(str, 1)
End If
End Function
Private Function ScanOnce(ByRef str, ByRef idx)
Dim c, ms
idx = SkipWhitespace(str, idx)
c = Mid(str, idx, 1)
If c = "{" Then
idx = idx + 1
Set ScanOnce = ParseObject(str, idx)
Exit Function
ElseIf c = "[" Then
idx = idx + 1
ScanOnce = ParseArray(str, idx)
Exit Function
ElseIf c = """" Then
idx = idx + 1
ScanOnce = ParseString(str, idx)
Exit Function
ElseIf c = "n" And StrComp("null", Mid(str, idx, 4)) = 0 Then
idx = idx + 4
ScanOnce = Null
Exit Function
ElseIf c = "t" And StrComp("true", Mid(str, idx, 4)) = 0 Then
idx = idx + 4
ScanOnce = True
Exit Function
ElseIf c = "f" And StrComp("false", Mid(str, idx, 5)) = 0 Then
idx = idx + 5
ScanOnce = False
Exit Function
End If
Set ms = NumberRegex.Execute(Mid(str, idx))
If ms.Count = 1 Then
idx = idx + ms(0).Length
ScanOnce = CDbl(ms(0))
Exit Function
End If
Err.Raise 8732,,"No JSON object could be ScanOnced"
End Function
Private Function ParseObject(ByRef str, ByRef idx)
Dim c, key, value
Set ParseObject = CreateObject("Scripting.Dictionary")
idx = SkipWhitespace(str, idx)
c = Mid(str, idx, 1)
If c = "}" Then
Exit Function
ElseIf c <> """" Then
Err.Raise 8732,,"Expecting property name"
End If
idx = idx + 1
Do
key = ParseString(str, idx)
idx = SkipWhitespace(str, idx)
If Mid(str, idx, 1) <> ":" Then
Err.Raise 8732,,"Expecting : delimiter"
End If
idx = SkipWhitespace(str, idx + 1)
If Mid(str, idx, 1) = "{" Then
Set value = ScanOnce(str, idx)
Else
value = ScanOnce(str, idx)
End If
ParseObject.Add key, value
idx = SkipWhitespace(str, idx)
c = Mid(str, idx, 1)
If c = "}" Then
Exit Do
ElseIf c <> "," Then
Err.Raise 8732,,"Expecting , delimiter"
End If
idx = SkipWhitespace(str, idx + 1)
c = Mid(str, idx, 1)
If c <> """" Then
Err.Raise 8732,,"Expecting property name"
End If
idx = idx + 1
Loop
idx = idx + 1
End Function
Private Function ParseArray(ByRef str, ByRef idx)
Dim c, values, value
Set values = CreateObject("Scripting.Dictionary")
idx = SkipWhitespace(str, idx)
c = Mid(str, idx, 1)
If c = "]" Then
ParseArray = values.Items
Exit Function
End If
Do
idx = SkipWhitespace(str, idx)
If Mid(str, idx, 1) = "{" Then
Set value = ScanOnce(str, idx)
Else
value = ScanOnce(str, idx)
End If
values.Add values.Count, value
idx = SkipWhitespace(str, idx)
c = Mid(str, idx, 1)
If c = "]" Then
Exit Do
ElseIf c <> "," Then
Err.Raise 8732,,"Expecting , delimiter"
End If
idx = idx + 1
Loop
idx = idx + 1
ParseArray = values.Items
End Function
Private Function ParseString(ByRef str, ByRef idx)
Dim chunks, content, terminator, ms, esc, char
Set chunks = CreateObject("Scripting.Dictionary")
Do
Set ms = StringChunk.Execute(Mid(str, idx))
If ms.Count = 0 Then
Err.Raise 8732,,"Unterminated string starting"
End If
content = ms(0).Submatches(0)
terminator = ms(0).Submatches(1)
If Len(content) > 0 Then
chunks.Add chunks.Count, content
End If
idx = idx + ms(0).Length
If terminator = """" Then
Exit Do
ElseIf terminator <> "\" Then
Err.Raise 8732,,"Invalid control character"
End If
esc = Mid(str, idx, 1)
If esc <> "u" Then
Select Case esc
Case """" char = """"
Case "\" char = "\"
Case "/" char = "/"
Case "b" char = b
Case "f" char = f
Case "n" char = n
Case "r" char = r
Case "t" char = t
Case Else Err.Raise 8732,,"Invalid escape"
End Select
idx = idx + 1
Else
char = ChrW("&H" & Mid(str, idx + 1, 4))
idx = idx + 5
End If
chunks.Add chunks.Count, char
Loop
ParseString = Join(chunks.Items, "")
End Function
Private Function SkipWhitespace(ByRef str, ByVal idx)
Do While idx <= Len(str) And _
InStr(Whitespace, Mid(str, idx, 1)) > 0
idx = idx + 1
Loop
SkipWhitespace = idx
End Function
End Class
VbsJson类
作者:恶魔
日期:2012年5月3日
"网址:http://demon.tw
私有空格、NumberRegex、StringChunk
二等兵b、f、r、n、t
私有子类_初始化
空格=“”&vbTab&vbCr&vbLf
b=ChrW(8)
f=vbFormFeed
r=vbCr
n=vbLf
t=vbTab
Set NumberRegex=New RegExp
NumberRegex.Pattern=“(?(?:0 |[1-9]\d*)(\.\d+)([eE][-+]?\d+)”
NumberRegex.Global=False
NumberRegex.MultiLine=True
NumberRegex.IgnoreCase=True
Set StringChunk=New RegExp
StringChunk.Pattern=“([\s\s]*?)([\\\x00-\x1f])”
StringChunk.Global=False
StringChunk.MultiLine=True
StringChunk.IgnoreCase=True
端接头
'返回VBScript数据结构的JSON字符串表示形式
'支持以下对象和类型
'+-------------------+---------------+
“| VBScript | JSON”|
'+===================+===============+
|字典|对象|
'+-------------------+---------------+
|数组|数组|
'+-------------------+---------------+
|字符串|字符串|
'+-------------------+---------------+
|编号|编号|
'+-------------------+---------------+
|对|对|
'+-------------------+---------------+
|假|假|
'+-------------------+---------------+
|空|空|
'+-------------------+---------------+
公共功能编码(ByRef obj)
暗buf,i,c,g
Set buf=CreateObject(“Scripting.Dictionary”)
选择案例变量类型(obj)
案例vbNull
buf.Add buf.Count,“空”
案例vbBoolean
如果是obj那么
buf.添加buf.计数,“真”
其他的
buf.添加buf.计数,“false”
如果结束
案例vbInteger、vbLong、vbSingle、vbDouble
基本单位加基本单位计数,对象
大小写字符串
buf.添加buf.计数“”“”
对于i=1至Len(obj)
c=Mid(obj,i,1)
选择案例c
案例“”基本单位。添加基本单位计数,\“”
大小写“\”buf.Add buf.Count,“\ \”
大小写“/”基本单位添加基本单位计数“/”
案例b基本单位。添加基本单位计数,“\b”
案例f buf.添加buf.计数,“\f”
案例r buf.添加buf.计数,“\r”
案例n基本单位。添加基本单位计数,“\n”
案例t buf.添加buf.计数,“\t”
其他情况
如果AscW(c)>=0且AscW(c)则JSON数据是对象/字典的数组。那么你的
Set jsonParsedUser = jsonClsUser.Decode(simonXmlResponse)
应该是:
jsonParsedUser = jsonClsUser.Decode(simonXmlResponse)
(Set
用于分配对象;请参阅)MyTestPage.asp中的哪一行是第83行?为什么这是离题的?OP已经提供了足够的信息,我很乐意编辑这个问题,使之成为主题,所以有人能告诉我,我遗漏了什么导致这个问题被关闭。导致“需要对象”错误的一个原因是使用集合指定非对象。json是一个字典数组,即非对象。移除集合,代码将“工作”(已测试)。至于“为什么我的问题结束了?”:有些人——他们中的大多数人从未回答过VBScript问题——在理解VBScript问题方面存在问题。