Note that there are some explanatory texts on larger screens.

plurals
  1. POExcel VBA: Parsed JSON Object Loop
    primarykey
    data
    text
    <p>Per example below...Looping through an object from a parsed JSON string returns an error "Object doesn't support this property or method". Could anyone advise how to make this work? Much appreciated (I spent 6 hours looking for an answer before asking here). </p> <p>Function to parse JSON string into object (this works OK).</p> <pre><code>Function jsonDecode(jsonString As Variant) Set sc = CreateObject("ScriptControl"): sc.Language = "JScript" Set jsonDecode = sc.Eval("(" + jsonString + ")") End Function </code></pre> <p>Looping through the parsed object returns error "Object doesn't support this property or method".</p> <pre><code>Sub TestJsonParsing() Dim arr As Object 'Parse the json array into here Dim jsonString As String 'This works fine jsonString = "{'key1':'value1','key2':'value2'}" Set arr = jsonDecode(jsonString) MsgBox arr.key1 'Works (as long as I know the key name) 'But this loop doesn't work - what am I doing wrong? For Each keyName In arr.keys 'Excel errors out here "Object doesn't support this property or method" MsgBox "keyName=" &amp; keyName MsgBox "keyValue=" &amp; arr(keyName) Next End Sub </code></pre> <p>PS. I looked into these libraries already:</p> <p>-<a href="http://code.google.com/p/vba-json/" rel="noreferrer">vba-json</a> Wasn't able to get the example working.<br> -<a href="http://www.ediy.co.nz/vbjson-json-parser-library-in-vb6-xidc55680.html" rel="noreferrer">VBJSON</a> There's no vba script included (this might work but don't know how to load it into Excel and there is minimum documentation).</p> <p>Also, Is it possible to access Multidimensional parsed JSON arrays? Just getting a single-dimension array loop working would be great (sorry if asking too much). Thanks. </p> <hr> <p>Edit: Here are two working examples using the vba-json library. The question above is still a mystery though...</p> <pre><code>Sub TestJsonDecode() 'This works, uses vba-json library Dim lib As New JSONLib 'Instantiate JSON class object Dim jsonParsedObj As Object 'Not needed jsonString = "{'key1':'val1','key2':'val2'}" Set jsonParsedObj = lib.parse(CStr(jsonString)) For Each keyName In jsonParsedObj.keys MsgBox "Keyname=" &amp; keyName &amp; "//Value=" &amp; jsonParsedObj(keyName) Next Set jsonParsedObj = Nothing Set lib = Nothing End Sub Sub TestJsonEncode() 'This works, uses vba-json library Dim lib As New JSONLib 'Instantiate JSON class object Set arr = CreateObject("Scripting.Dictionary") arr("key1") = "val1" arr("key2") = "val2" MsgBox lib.toString(arr) End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload