2013-06-21 79 views
2

所以我使用VBScript代碼,通過我的COM對象,以便在第一我VBs的代碼來解碼JSON文件 是:VBScript的對象撰寫

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"} 

而且我正在就好了「FALS E「

但後來,我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」),但空是什麼我得到了...

所以我用了
結果檢查結果 ,結果是(「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] 

一段時間後,試圖找出什麼這樣做,因爲我不能用一個簡單的數字訪問子對象(0.1,0.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」(get 1 sub object.title)。
也可以有多個子維... jsons ....

而且我必須使用VBScript和NOT JScript。它在組合對象中使用。
使用JScript無法使用UTF8打開文件。在VBScript中,它與ADODB.Stream

+0

我真的認爲你可能能夠適應在這裏找到的類:http://www.aspjson.com/;只需要關閉服務器對象,我認爲它應該是好的。 – dennythecoder

+0

您可以使用['ActiveXObject'](http://msdn.microsoft.com/zh-cn/library/7sw4ddf8%28v=vs.84%29.aspx)在JScript中使用COM對象。假設您不在安全沙箱中(即Web瀏覽器),您應該能夠像使用VBScript一樣在JScript中使用ADODB.Stream。 –

回答

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