使用VBscript访问JSON数据中的所有值
我必须做一些vbscript来处理来自Web服务器的json格式的输出。我使用的是我发现的一个名为“aspJSON”的旧vbscript代码片段——我想它来自www.aspJSON.com,但该网站已不再可用 我有这个JSON文件:使用VBscript访问JSON数据中的所有值,json,vbscript,Json,Vbscript,我必须做一些vbscript来处理来自Web服务器的json格式的输出。我使用的是我发现的一个名为“aspJSON”的旧vbscript代码片段——我想它来自www.aspJSON.com,但该网站已不再可用 我有这个JSON文件: { "VAT":12678967.543233, "buyInfo":{ "maximumBuyAmount":100, "minimumBuyAmount":1, }, "prices":[{ "unitPrice":12.50 "
{
"VAT":12678967.543233,
"buyInfo":{
"maximumBuyAmount":100,
"minimumBuyAmount":1,
},
"prices":[{
"unitPrice":12.50
"specialOfferPrice":8.75,
"period":{
"endDate":"\/Date(928142400000+0200)\/",
"startDate":"\/Date(928142400000+0200)\/",
},
}],
}
使用aspJSON代码,我可以从数据中获取一些值。这两种方法很好:
Msgbox oJSON.data("VAT")
MsgBox oJSON.data("buyInfo").item("maximumBuyAmount")
但我似乎无法理解价格的价值:
[{"unitPrice":12.50}]
和期间
:
[{"period":{"endDate":"xxx"}}]
如何访问这些值
这是aspJSON代码:
'Februari 2014 - Version 1.17 by Gerrit van Kuipers
Class aspJSON
Public data
Private p_JSONstring
private aj_in_string, aj_in_escape, aj_i_tmp, aj_char_tmp, aj_s_tmp, aj_line_tmp, aj_line, aj_lines, aj_currentlevel, aj_currentkey, aj_currentvalue, aj_newlabel, aj_XmlHttp, aj_RegExp, aj_colonfound
Private Sub Class_Initialize()
Set data = Collection()
Set aj_RegExp = new regexp
aj_RegExp.Pattern = "\s{0,}(\S{1}[\s,\S]*\S{1})\s{0,}"
aj_RegExp.Global = False
aj_RegExp.IgnoreCase = True
aj_RegExp.Multiline = True
End Sub
Private Sub Class_Terminate()
Set data = Nothing
Set aj_RegExp = Nothing
End Sub
Public Sub loadJSON(inputsource)
inputsource = aj_MultilineTrim(inputsource)
If Len(inputsource) = 0 Then Err.Raise 1, "loadJSON Error", "No data to load."
select case Left(inputsource, 1)
case "{", "["
case else
Set aj_XmlHttp = CreateObject("Msxml2.ServerXMLHTTP")
aj_XmlHttp.open "GET", inputsource, False
aj_XmlHttp.setRequestHeader "Content-Type", "text/json"
aj_XmlHttp.setRequestHeader "CharSet", "UTF-8"
aj_XmlHttp.Send
inputsource = aj_XmlHttp.responseText
set aj_XmlHttp = Nothing
end select
p_JSONstring = CleanUpJSONstring(inputsource)
aj_lines = Split(p_JSONstring, Chr(13) & Chr(10))
Dim level(99)
aj_currentlevel = 1
Set level(aj_currentlevel) = data
For Each aj_line In aj_lines
aj_currentkey = ""
aj_currentvalue = ""
If Instr(aj_line, ":") > 0 Then
aj_in_string = False
aj_in_escape = False
aj_colonfound = False
For aj_i_tmp = 1 To Len(aj_line)
If aj_in_escape Then
aj_in_escape = False
Else
Select Case Mid(aj_line, aj_i_tmp, 1)
Case """"
aj_in_string = Not aj_in_string
Case ":"
If Not aj_in_escape And Not aj_in_string Then
aj_currentkey = Left(aj_line, aj_i_tmp - 1)
aj_currentvalue = Mid(aj_line, aj_i_tmp + 1)
aj_colonfound = True
Exit For
End If
Case "\"
aj_in_escape = True
End Select
End If
Next
if aj_colonfound then
aj_currentkey = aj_Strip(aj_JSONDecode(aj_currentkey), """")
If Not level(aj_currentlevel).exists(aj_currentkey) Then level(aj_currentlevel).Add aj_currentkey, ""
end if
End If
If right(aj_line,1) = "{" Or right(aj_line,1) = "[" Then
If Len(aj_currentkey) = 0 Then aj_currentkey = level(aj_currentlevel).Count
Set level(aj_currentlevel).Item(aj_currentkey) = Collection()
Set level(aj_currentlevel + 1) = level(aj_currentlevel).Item(aj_currentkey)
aj_currentlevel = aj_currentlevel + 1
aj_currentkey = ""
ElseIf right(aj_line,1) = "}" Or right(aj_line,1) = "]" or right(aj_line,2) = "}," Or right(aj_line,2) = "]," Then
aj_currentlevel = aj_currentlevel - 1
ElseIf Len(Trim(aj_line)) > 0 Then
if Len(aj_currentvalue) = 0 Then aj_currentvalue = aj_line
aj_currentvalue = getJSONValue(aj_currentvalue)
If Len(aj_currentkey) = 0 Then aj_currentkey = level(aj_currentlevel).Count
level(aj_currentlevel).Item(aj_currentkey) = aj_currentvalue
End If
Next
End Sub
Public Function Collection()
set Collection = CreateObject("Scripting.Dictionary")
End Function
Public Function AddToCollection(dictobj)
if TypeName(dictobj) <> "Dictionary" then Err.Raise 1, "AddToCollection Error", "Not a collection."
aj_newlabel = dictobj.Count
dictobj.Add aj_newlabel, Collection()
set AddToCollection = dictobj.item(aj_newlabel)
end function
Private Function CleanUpJSONstring(aj_originalstring)
aj_originalstring = Replace(aj_originalstring, Chr(13) & Chr(10), "")
aj_originalstring = Mid(aj_originalstring, 2, Len(aj_originalstring) - 2)
aj_in_string = False : aj_in_escape = False : aj_s_tmp = ""
For aj_i_tmp = 1 To Len(aj_originalstring)
aj_char_tmp = Mid(aj_originalstring, aj_i_tmp, 1)
If aj_in_escape Then
aj_in_escape = False
aj_s_tmp = aj_s_tmp & aj_char_tmp
Else
Select Case aj_char_tmp
Case "\" : aj_s_tmp = aj_s_tmp & aj_char_tmp : aj_in_escape = True
Case """" : aj_s_tmp = aj_s_tmp & aj_char_tmp : aj_in_string = Not aj_in_string
Case "{", "["
aj_s_tmp = aj_s_tmp & aj_char_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10))
Case "}", "]"
aj_s_tmp = aj_s_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10)) & aj_char_tmp
Case "," : aj_s_tmp = aj_s_tmp & aj_char_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10))
Case Else : aj_s_tmp = aj_s_tmp & aj_char_tmp
End Select
End If
Next
CleanUpJSONstring = ""
aj_s_tmp = split(aj_s_tmp, Chr(13) & Chr(10))
For Each aj_line_tmp In aj_s_tmp
aj_line_tmp = replace(replace(aj_line_tmp, chr(10), ""), chr(13), "")
CleanUpJSONstring = CleanUpJSONstring & aj_Trim(aj_line_tmp) & Chr(13) & Chr(10)
Next
End Function
Private Function getJSONValue(ByVal val)
val = Trim(val)
If Left(val,1) = ":" Then val = Mid(val, 2)
If Right(val,1) = "," Then val = Left(val, Len(val) - 1)
val = Trim(val)
Select Case val
Case "true" : getJSONValue = True
Case "false" : getJSONValue = False
Case "null" : getJSONValue = Null
Case Else
If (Instr(val, """") = 0) Then
If IsNumeric(val) Then
getJSONValue = CDbl(val)
Else
getJSONValue = val
End If
Else
If Left(val,1) = """" Then val = Mid(val, 2)
If Right(val,1) = """" Then val = Left(val, Len(val) - 1)
getJSONValue = aj_JSONDecode(Trim(val))
End If
End Select
End Function
Private JSONoutput_level
Public Function JSONoutput()
dim wrap_dicttype, aj_label
JSONoutput_level = 1
wrap_dicttype = "[]"
For Each aj_label In data
If Not aj_IsInt(aj_label) Then wrap_dicttype = "{}"
Next
JSONoutput = Left(wrap_dicttype, 1) & Chr(13) & Chr(10) & GetDict(data) & Right(wrap_dicttype, 1)
End Function
Private Function GetDict(objDict)
dim aj_item, aj_keyvals, aj_label, aj_dicttype
For Each aj_item In objDict
Select Case TypeName(objDict.Item(aj_item))
Case "Dictionary"
GetDict = GetDict & Space(JSONoutput_level * 4)
aj_dicttype = "[]"
For Each aj_label In objDict.Item(aj_item).Keys
If Not aj_IsInt(aj_label) Then aj_dicttype = "{}"
Next
If aj_IsInt(aj_item) Then
GetDict = GetDict & (Left(aj_dicttype,1) & Chr(13) & Chr(10))
Else
GetDict = GetDict & ("""" & aj_JSONEncode(aj_item) & """" & ": " & Left(aj_dicttype,1) & Chr(13) & Chr(10))
End If
JSONoutput_level = JSONoutput_level + 1
aj_keyvals = objDict.Keys
GetDict = GetDict & (GetSubDict(objDict.Item(aj_item)) & Space(JSONoutput_level * 4) & Right(aj_dicttype,1) & aj_InlineIf(aj_item = aj_keyvals(objDict.Count - 1),"" , ",") & Chr(13) & Chr(10))
Case Else
aj_keyvals = objDict.Keys
GetDict = GetDict & (Space(JSONoutput_level * 4) & aj_InlineIf(aj_IsInt(aj_item), "", """" & aj_JSONEncode(aj_item) & """: ") & WriteValue(objDict.Item(aj_item)) & aj_InlineIf(aj_item = aj_keyvals(objDict.Count - 1),"" , ",") & Chr(13) & Chr(10))
End Select
Next
End Function
Private Function aj_IsInt(val)
aj_IsInt = (TypeName(val) = "Integer" Or TypeName(val) = "Long")
End Function
Private Function GetSubDict(objSubDict)
GetSubDict = GetDict(objSubDict)
JSONoutput_level= JSONoutput_level -1
End Function
Private Function WriteValue(ByVal val)
Select Case TypeName(val)
Case "Double", "Integer", "Long": WriteValue = val
Case "Null" : WriteValue = "null"
Case "Boolean" : WriteValue = aj_InlineIf(val, "true", "false")
Case Else : WriteValue = """" & aj_JSONEncode(val) & """"
End Select
End Function
Private Function aj_JSONEncode(ByVal val)
val = Replace(val, "\", "\\")
val = Replace(val, """", "\""")
'val = Replace(val, "/", "\/")
val = Replace(val, Chr(8), "\b")
val = Replace(val, Chr(12), "\f")
val = Replace(val, Chr(10), "\n")
val = Replace(val, Chr(13), "\r")
val = Replace(val, Chr(9), "\t")
aj_JSONEncode = Trim(val)
End Function
Private Function aj_JSONDecode(ByVal val)
val = Replace(val, "\""", """")
val = Replace(val, "\\", "\")
val = Replace(val, "\/", "/")
val = Replace(val, "\b", Chr(8))
val = Replace(val, "\f", Chr(12))
val = Replace(val, "\n", Chr(10))
val = Replace(val, "\r", Chr(13))
val = Replace(val, "\t", Chr(9))
aj_JSONDecode = Trim(val)
End Function
Private Function aj_InlineIf(condition, returntrue, returnfalse)
If condition Then aj_InlineIf = returntrue Else aj_InlineIf = returnfalse
End Function
Private Function aj_Strip(ByVal val, stripper)
If Left(val, 1) = stripper Then val = Mid(val, 2)
If Right(val, 1) = stripper Then val = Left(val, Len(val) - 1)
aj_Strip = val
End Function
Private Function aj_MultilineTrim(TextData)
aj_MultilineTrim = aj_RegExp.Replace(TextData, "$1")
End Function
private function aj_Trim(val)
aj_Trim = Trim(val)
Do While Left(aj_Trim, 1) = Chr(9) : aj_Trim = Mid(aj_Trim, 2) : Loop
Do While Right(aj_Trim, 1) = Chr(9) : aj_Trim = Left(aj_Trim, Len(aj_Trim) - 1) : Loop
aj_Trim = Trim(aj_Trim)
end function
End Class
2014年2月-Gerrit van Kuipers的1.17版
类aspJSON
公共数据
私人p_JSONstring
专用aj_in_字符串、aj_in_escape、aj_i_tmp、aj_char_tmp、aj_s_tmp、aj_line_tmp、aj_line、aj_lines、aj_currentlevel、aj_currentkey、aj_currentvalue、aj_newlabel、aj_XmlHttp、aj_RegExp、aj_colonfound
私有子类_Initialize()
集合数据=集合()
设置aj_RegExp=新RegExp
aj_RegExp.Pattern=“\s{0,}(\s{1}[\s\s]*\s{1})\s{0,}”
aj_RegExp.Global=False
aj_RegExp.IgnoreCase=True
aj_RegExp.Multiline=True
端接头
私有子类_Terminate()
设置数据=无
设置aj_RegExp=Nothing
端接头
公共子加载JSON(inputsource)
inputsource=aj_多指标(inputsource)
如果Len(inputsource)=0,则错误提示1,“loadJSON错误”,“无需加载数据”
选择案例左侧(inputsource,1)
格{,“[”
其他情况
设置aj_XmlHttp=CreateObject(“Msxml2.ServerXMLHTTP”)
aj_XmlHttp.open“GET”,inputsource,False
aj_XmlHttp.setRequestHeader“内容类型”、“文本/json”
aj_XmlHttp.setRequestHeader“字符集”、“UTF-8”
aj_XmlHttp.Send
inputsource=aj_XmlHttp.responseText
设置aj_XmlHttp=Nothing
结束选择
p_JSONstring=CleanUpJSONstring(inputsource)
aj_线=拆分(p_JSONstring、Chr(13)和Chr(10))
暗电平(99)
aj_电流电平=1
设置电平(aj_currentlevel)=数据
对于aj_线中的每个aj_线
aj_currentkey=“”
aj_currentvalue=“”
如果仪表(aj_线,“:”)大于0,则
aj_in_字符串=False
aj_in_escape=错误
aj_colonfound=False
对于aj_i_tmp=1至Len(aj_线)
如果aj_在_逃跑,那么
aj_in_escape=错误
其他的
选择案例中间(aj_线,aj_i_tmp,1)
案例“”“”
aj_in_字符串=非aj_in_字符串
案例“:”
如果不是aj_in_escape,也不是aj_in_string,则
aj_电流键=左(aj_线,aj_i_tmp-1)
aj_电流值=中间(aj_线,aj_i_tmp+1)
aj_colonfound=True
退出
如果结束
案例“\”
aj_in_escape=真
结束选择
如果结束
下一个
如果aj_colonfound那么
aj_currentkey=aj_条带(aj_JSONDecode(aj_currentkey),“”)
如果没有级别(aj_currentlevel)。存在(aj_currentkey),则为级别(aj_currentlevel)。添加aj_currentkey“
如果结束
如果结束
如果right(aj_线,1)=“{”或right(aj_线,1)=“[”则
如果Len(aj_currentkey)=0,则aj_currentkey=电平(aj_currentlevel)。计数
设置级别(aj_currentlevel)。项(aj_currentkey)=集合()
设置级别(aj_currentlevel+1)=级别(aj_currentlevel)。项目(aj_currentkey)
aj_currentlevel=aj_currentlevel+1
aj_currentkey=“”
如果右(aj_线,1)=“}”或右(aj_线,1)=“]”或右(aj_线,2)=“}”,或右(aj_线,2)=“]”,则
aj_currentlevel=aj_currentlevel-1
ElseIf Len(修剪(aj_线))>0然后
如果Len(aj_currentvalue)=0,则aj_currentvalue=aj_线
aj_currentvalue=getJSONValue(aj_currentvalue)
如果Len(aj_currentkey)=0,则aj_currentkey=电平(aj_currentlevel)。计数
级别(aj_currentlevel)。项目(aj_currentkey)=aj_currentvalue
如果结束
下一个
端接头
公共功能集合()
set Collection=CreateObject(“Scripting.Dictionary”)
端函数
公共功能添加到集合(dictobj)
如果TypeName(dictobj)“Dictionary”,则Err.Raise 1,“AddToCollection Error”,“Not a collection.”
aj_newlabel=dictobj.Count
dictobj.添加aj_新标签,集合()
设置AddToCollection=dictobj.item(aj_newlabel)
端函数
私有函数CleanUpJSONstring(aj_originalstring)
aj_originalstring=替换(aj_originalstring,Chr(13)和Chr(10),“”)
aj_originalstring=Mid(aj_originalstring,2,Len(aj_originalstring)-2)
aj_in_string=False:aj_in_escape=False:aj_s_tmp=“”
对于aj_i_tmp=1到Len(aj_原始字符串)
aj_char_tmp=Mid(aj_originalstring,aj_i_tmp,1)
如果aj_在_逃跑,那么
aj_in_escape=错误
aj_s_tmp=aj_s_tmp&aj_char_tmp
其他的
选择案例aj\U char\U tmp
案例“\”:aj_s_tmp=aj_s_tmp&aj_char_tmp:aj_in_escape=True
案例“”:aj_s_tmp=aj_s_tmp&aj_char_tmp:aj_in_string=非aj_in_string
格{,“[”
aj_s_tmp=aj_s_tmp&aj_char_tmp&aj_InlineIf(aj_in_string,“,Chr(13)和Chr(10))
案例“}”,案例“]
aj_s_tmp=aj_s_tmp&aj_InlineIf(aj_in_string,“,Chr(13)和Chr(10))&aj_char_tmp
大小写“,”:aj_s_tmp=aj_s_tmp&aj_char_tmp&aj_InlineIf(aj_in_string,”,Chr(13)和Chr(10))
其他情况:aj_s_tmp=aj_s_tmp&aj_char_tmp
结束选择
如果结束
下一个
CleanUpJSONstring=“”
aj___tmp=分割(aj__tmp,Chr(13)和Chr(10))
对于aj_s_tmp中的每个aj_线_tmp
A.
Dim key, price
'Iterating a Scripting.Dictionary using For Each returns the key.
For Each key In oJSON.data("prices")
'Get the price instance by passing the key back into
'the Scripting.Dictionary.
Set price = oJSON.data("prices")(key)
MsgBox price.item("unitPrice")
MsgBox price.item("specialOfferPrice")
MsgBox price.item("period").item("endDate")
MsgBox price.item("period").item("startDate")
'Clear object before iterating the next instance.
Set price = Nothing
Next
oJSON("prices")(0).Item("unitPrice")
Option Explicit
Dim prices: Set prices = CreateObject("Scripting.Dictionary")
Dim price, period
With prices
Set price = CreateObject("Scripting.Dictionary")
With price
Call .Add("unitPrice", 12.50)
Call .Add("specialOfferPrice", 8.75)
Set period = CreateObject("Scripting.Dictionary")
With period
Call .Add("endDate", "/Date(928142400000+0200)/")
End With
Call .Add("period", period)
End With
'Uses same method as the AddToCollection() in aspJSON to
'assign the ordinal position when adding the child Dictionary.
Call .Add(.Count, price)
End With
WScript.Echo prices(0).Item("unitPrice")
WScript.Echo prices(0).Item("period").Item("endDate")