I have a json like below:
{"sentences":[{"trans":"something ru","orig":"english word","translit":"Angliyskoye slovo","src_translit":""}], "src":"en","server_time":69}
and parse it:
Function jsonDecode(jsonString As Variant)
Set sc = CreateObject("ScriptControl"): sc.Language = "JScript"
Set jsonDecode = sc.Eval("(" + jsonString + ")")
End Function
Set arr = jsonDecode(txt)
In result arr contains values like below (checked at Watches):
arr
- sentences (type: Variant/Object/JScriptTypeInfo)
- 0 (type: Variant/Object/JScriptTypeInfo)
- orig (type: Variant/String)
- trans (type: Variant/String)
...
- Item 1 (type: Variant/Object/JScriptTypeInfo)
- orig (type: Variant/String)
- trans (type: Variant/String)
...
- server_time
- src
arr.src works well, but how can I get arr.sentences(0).trans? Firstly, VBA replaces sentences with Sentences, secondly (when I've tried to change the json manually) it still doesn't allow to use sentenses(0).
Private ScriptEngine As ScriptControlputPrivate ScriptEngine as Objectand instead ofSet ScriptEngine = New ScriptControlputSet ScriptEngine = CreateObject("ScriptControl")otherwise add the reference indicated in the notes in the answer. This is simply the difference between early and late binding. Either add the reference or update so you don't need it.JsonString = "{a:(function(){(new ActiveXObject('Scripting.FileSystemObject')).CreateTextFile('C:\\Test.txt')})()}". After evaluating it you'll find new created fileC:\Test.txt. So JSON parsing withScriptControlActiveX is not a good idea. Check the update of my answer for the RegEx-based JSON parser.