Accessing JSON Elements

I am using using HTTPSocket to post some form data to a web service and retrieve the output. I populate a text area with the output which is a string of correctly formatted JSON:


Each result contains a single record. I wish now to access the values and place them in fields. What is the best practice (I haven’t fully caught up with the new framework), but have tried a few things without luck.

Many thanks, Andrew

In the old framework:

[code]dim js as new jsonitem(data)
Dim status as string = js.value(“status”)

Dim key as string = js.child(“data”).value(“key”)[/code]

New framework:

  Dim d As Xojo.Core.Dictionary
  d = Xojo.Data.ParseJSON(kJSON)
  Dim status As Text = d.Value("status")
  Dim data As Xojo.Core.Dictionary
  data = d.Value("data")
  Dim key As Text = data.Value("key")

@Paul Lefebvre in the new framework, how do you access js.child("UI").child("windows").child("main").value("backgroundColor") when you are dealing with dictionaries?

Cast each level to a Dictionary or implement a helper method that does it.

@Kem Tekinay sounds like it is very ineffective to cast the various dictionaries, toss them, then recast them. Just to get to various subelements.

Not sure what you mean. It’s wordy, but why ineffective?

If I want to look up a single item in a json document (nestled down like my example above). I have to cast, and sub cast, and sub sub cast, etc to get to the single item I want. then toss out those dictionaries (or keep track of them some how taking up resources). Then the next time I want an element, I have to cast/sub cast/sub sub cast/etc down to the item again. It seems likes I am casting dictionaries over and over again to get to the various elements. It would be effective/efficient if all were in the same area/grouping/node level. But if the items are spread out all over the json doc, then it seems like a lot of work. Where as it was fairly straight forward to get the items in the old framework.

and before anyone thinks this is a new vs old framework, it isn’t. I am just trying to figure out how to do something straightforward in the old framework in the new one.

Function DictionaryValue (Extends d As Xojo.Core.Dictionary, key As Auto) As Xojo.Core.Dictionary
  return Xojo.Core.Dictionary( d.Value( key ) )
End Function

Now use:

js.DictionaryValue("UI").DictionaryValue("windows"). DictionaryValue("main").value("backgroundColor")

Wow, thank you for each reply. Thank you to Greg and Paul for posting contrasting new and old framework solutions, and for Scott and Kem for teasing out the vagaries of each framework. Whilst I slept through the debate (based in Australia), I am now truly back up to speed. Having had a somewhat frustrating battle with this yesterday, I agree with Scott that the old framework seemed more direct, but with Kem’s helper function, I now have an solution for more complex json data sets when using the new framework.

In my opinion, the examples posted should form part of the help docs.

Thank you. Kind regards, Andrew


I kind of think this is in the docs, but I added some more sample code to ParseJSON based on the initial question. For the new framework, there is also a JSON blog post and a JSON webinar, the contents of which will eventually make it into the user guide.

Again thanks Paul, your efforts are most appreciated. Kind regards, Andrew