So i'm using a vbscript code through my Com Objects in order to decode a json file at first my VBs code was:
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
So i was just taking every value i wanted just with
result("movie.json",".adult") for example:
With Json:
{"adult":false,"backdrop_path":"/hbn46fQaRmlpBuUrEiFqv0GDL6Y.jpg"}
And i was taking just fine the "false"
But then , my jsons got more tricky...
{"adult":false,"backdrop_path":"/hbn46fQaRmlpBuUrEiFqv0GDL6Y.jpg","belongs_to_collection":{"id":86311,"name":"The Avengers Collection","poster_path":"/qJawKUQcIBha507UahUlX0keOT7.jpg","backdrop_path":"/zuW6fOiusv4X9nnW3paHGfXcSll.jpg"}}
But still i was able to get data from there with my second argument: ".belongs_to_collection.id" Until THIS:
{
"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"
}
]
}
}
And i tried to get one of the alternative titles, i tried with my default method...
result("movie.json",".alternative_titles.titles.0.title") but null was what i got...
So i used it for
result("movie.json",".alternative_titles.titles") to check the result
and the result was:
[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]
After some time trying to figure out what to do since i couldn't access the sub object with a simple number (.1 , .0 ...) i tried to create a function to do things a bit more easier, though failed:
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
Any ideas how to manage to Get the value i want by just having the same input?
"alternative_titles.titles.0.title" (get 1 sub object.title).
Also it could have more than one sub dimension...jsons....
And i HAVE to use VBScript and NOT JScript. It used within combojects.
with JScript its not able to open a file with UTF8. IN VBScript it is with ADODB.Stream
sometime ago, I wrote a JSON to XMLDOM converter (see Decode/Encode JSON with VBScript ). When applied to your JSON, it produces the following 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>
As an XMLDOM it should be a simple matter of using XPath queries and selectSingleNode to get the values you want. For example: Select Single Node with a attribute name in vbscript