2

所以我通过我的 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 一起使用

4

1 回答 1

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 来获取您想要的值应该是一件简单的事情。例如:在 vbscript 中选择具有属性名称的 Single Node

于 2013-09-16T05:19:24.890 回答