所以我通过我的Com Objects使用vbscript代码来解码json文件 起初我的VB代码是:
Function filejson(json)
On Error Resume Next
Dim objStream, strData
Set objStream = CreateObject("ADODB.Stream")
objStream.CharSet = "utf-8"
objStream.Open
objStream.LoadFromFile(json)
strData = objStream.ReadText()
filejson=strData
End Function
Function str2json(json,value)
On Error Resume Next
Set scriptControl = CreateObject("MSScriptControl.ScriptControl")
scriptControl.Language = "JScript"
Set searchResultObject = scriptControl.Eval("(" + json + ")")
str2json=Eval("searchResultObject" & value )
End Function
Function result(json,value)
On Error Resume Next
result=str2json(filejson(json),value)
End Function
所以我只是用
来获取我想要的每一个价值result("movie.json",".adult") for example:
Json:
{"adult":false,"backdrop_path":"/hbn46fQaRmlpBuUrEiFqv0GDL6Y.jpg"}
我正好接受“假”
然而,我的jsons变得更加棘手......
{"adult":false,"backdrop_path":"/hbn46fQaRmlpBuUrEiFqv0GDL6Y.jpg","belongs_to_collection":{"id":86311,"name":"The Avengers Collection","poster_path":"/qJawKUQcIBha507UahUlX0keOT7.jpg","backdrop_path":"/zuW6fOiusv4X9nnW3paHGfXcSll.jpg"}}
但我仍然可以通过我的第二个参数从那里获取数据:“。belongs_to_collection.id”直到这个:
{
"adult": false,
"backdrop_path": "/hbn46fQaRmlpBuUrEiFqv0GDL6Y.jpg",
"belongs_to_collection": {
"id": 86311,
"name": "The Avengers Collection",
"poster_path": "/qJawKUQcIBha507UahUlX0keOT7.jpg",
"backdrop_path": "/zuW6fOiusv4X9nnW3paHGfXcSll.jpg"
},
"alternative_titles": {
"titles": [
{
"iso_3166_1": "IT",
"title": "I vendicatori"
},
{
"iso_3166_1": "BR",
"title": "Os Vingadores"
},
{
"iso_3166_1": "GB",
"title": "Avengers Assemble"
},
{
"iso_3166_1": "US",
"title": "Marvel's The Avengers"
},
{
"iso_3166_1": "SE",
"title": "Avengers 3D"
},
{
"iso_3166_1": "ES",
"title": "Marvel Los Vengadores"
},
{
"iso_3166_1": "PL",
"title": "Avengers 3D"
},
{
"iso_3166_1": "IL",
"title": "הנוקמים"
},
{
"iso_3166_1": "US",
"title": "The Avengers 3D"
},
{
"iso_3166_1": "CZ",
"title": "Avengers"
},
{
"iso_3166_1": "TW",
"title": "復仇者聯盟"
},
{
"iso_3166_1": "DE",
"title": "Marvel's The Avengers - Die Rächer"
},
{
"iso_3166_1": "DE",
"title": "The Avengers - Die Rächer"
},
{
"iso_3166_1": "VE",
"title": "Los Vengadores"
}
]
}
}
我试图获得其中一个替代标题,我尝试使用我的默认方法...
结果(“movie.json”,“。alternative_titles.titles.0.title”)但是null是我得到的......
所以我用它来做 结果(“movie.json”,“。alternative_titles.titles”)来检查结果 结果是:
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
经过一段时间试图弄清楚要做什么,因为我无法使用简单的数字(.1,.0 ......)访问子对象,我试图创建一个更容易做事的函数,虽然失败了:
Function filejson(json)
On Error Resume Next
Dim objStream, strData
Set objStream = CreateObject("ADODB.Stream")
objStream.CharSet = "utf-8"
objStream.Open
objStream.LoadFromFile(json)
strData = objStream.ReadText()
filejson=strData
End Function
Function str2json(json,value)
On Error Resume Next
Set scriptControl = CreateObject("MSScriptControl.ScriptControl")
scriptControl.Language = "JScript"
Set searchResultObject = scriptControl.Eval("(" + json + ")")
parameters=Split(value,".")
fullparm="obj"
obj=Eval("searchResultObject" & fullparm )
For Each parm in parameters
MsgBox("Parameter: "&parm&" | Old Object: "& obj)
If Eval("obj."&parm) = "[object Object]" Then
If IsNumeric(parm) Then
i=0
For Each new_obj in obj
If Trim(i) = Trim(parm) then
MsgBox("New Object: " & "obj | Value: " & new_obj)
obj=new_obj
fullparm="obj."
End If
i=i+1
Next
Else
obj=Eval("obj." & parm)
fullparm=fullparm&"."&parm
MsgBox("New Object: " & "obj." & parm & " | Value: " & obj)
End If
Else
str2json=obj
Exit Function
End If
Next
MsgBox(fullparm)
str2json="false"
End Function
Function result(json,value)
On Error Resume Next
result=str2json(filejson(json),value)
End Function
任何想法如何通过输入相同的方式来获得我想要的价值?
“alternative_titles.titles.0.title”(获得1个sub object.title)。
它也可能有多个子维度... jsons ....
我必须使用VBScript而不是JScript。它在组合中使用。
使用JScript无法使用UTF8打开文件。在VBScript中,它与ADODB.Stream
答案 0 :(得分:0)
前一段时间,我写了一个JSON到XMLDOM转换器(参见Decode/Encode JSON with VBScript )。应用于JSON时,它会生成以下XMLDOM:
<OBJECT adult="false" backdrop_path="/hbn46fQaRmlpBuUrEiFqv0GDL6Y.jpg">
<OBJECT id="86311" name="The Avengers Collection" poster_path="/qJawKUQcIBha507UahUlX0keOT7.jpg" backdrop_path="/zuW6fOiusv4X9nnW3paHGfXcSll.jpg"/>
<OBJECT>
<ARRAY>
<OBJECT iso_3166_1="IT" title="I vendicatori"/>
<OBJECT iso_3166_1="BR" title="Os Vingadores"/>
<OBJECT iso_3166_1="GB" title="Avengers Assemble"/>
<OBJECT iso_3166_1="US" title="Marvel's The Avengers"/>
<OBJECT iso_3166_1="SE" title="Avengers 3D"/>
<OBJECT iso_3166_1="ES" title="Marvel Los Vengadores"/>
<OBJECT iso_3166_1="PL" title="Avengers 3D"/>
<OBJECT iso_3166_1="IL" title="הנוקמים"/>
<OBJECT iso_3166_1="US" title="The Avengers 3D"/>
<OBJECT iso_3166_1="CZ" title="Avengers"/>
<OBJECT iso_3166_1="TW" title="復仇者聯盟"/>
<OBJECT iso_3166_1="DE" title="Marvel's The Avengers - Die Rächer"/>
<OBJECT iso_3166_1="DE" title="The Avengers - Die Rächer"/>
<OBJECT iso_3166_1="VE" title="Los Vengadores"/>
</ARRAY>
</OBJECT>
</OBJECT>
作为XMLDOM,使用XPath查询和selectSingleNode来获取所需的值应该是一个简单的问题。例如:Select Single Node with a attribute name in vbscript