所以我通过我的 Com 对象使用 vbscript 代码来解码一个 json 文件,起初我的 VBs 代码是:
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......
所以我将它用于
result("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 个子 object.title)。
它也可以有多个子维度...jsons....
而且我必须使用 VBScript 而不是 JScript。它在组合项目中使用。
使用 JScript 无法打开带有 UTF8 的文件。在 VBScript 中,它与 ADODB.Stream 一起使用