How to access subelements in Json object?


I was able to convert a JSON string into a dictionary. With simple structure like I can get each element value by .Value, so np…

In the end I want/need a more complex structure like:

[code]Var jsonText As String = “{”“town”":{ ““City””:"“Boston”","“Team”":"“Red Sox”"} }

Var mydict As Dictionary
mydict = ParseJSON(jsonText)

Target: I simply want to get value of f.e. element “Team” (-> “Red Sox”)

The problem: All my tries leads to an error message or jumps into the debug mode. Using Json in Javascript is a nobrainer as you use the dot-notation. I don’t understand why this is such a problem (even if possible) in Xojo. I couldn’t find any example showing JSON access.

These examples do not work:

[code]// Direct way
MessageBox ( mydict.Value(“City”) )

// Through hiearchy
MessageBox ( mydict.Value(“town”).Value(“City”) )

// Dot notation
MessageBox (“City”) )[/code]

So general question: How (if) can I access sub-elements in a JSON object?

dim sub as Dictionary = mydict.Value( "town" )
MessageBox sub.Value( "City" )

Woooot, fast and correct answer, you rock, thank you! I’m glad to have a solution. I’m not so happy I have to do it that way, but ok…

I recognised two problems: “sub” is a keyword, can’t be used as variable and MessageBox wants brackets.

For others who have the same problem, here two complete working samples with one and two hierarchies:

[code]// Sample for JSON acces with one hierarchy
Var jsonText As String = “{”“City”":"“Boston”","“Team”":"“Red Sox”"}"

Var mydict As Dictionary
mydict = ParseJSON(jsonText)

MessageBox ( mydict.Value(“City”) )[/code]

[code]// Sample for JSON acces with two hierarchies
Var jsonText As String = “{”“Town”":{ ““City””:"“Boston”","“Team”":"“Red Sox”"} }"

Var mydict As Dictionary
mydict = ParseJSON(jsonText)

Var mydictsub As Dictionary = mydict.Value( “Town” )
MessageBox ( mydictsub.Value(“City”) )[/code]

Yeah, I didn’t test, just wanted to give you an idea of how to do it.

All good, I thought so and I could get the point out of your code sample!

Dot notation like in Javascript does not xist in Xojo’s implementation of JSON
Not that it couldnt - it just doesnt
They could add runtime dot notation using operator_lookup
But that doesnt help at compile time :frowning:

@Norman Palardy Agree, otherwise I would have found it in the documentation. I’m aware that Xojo demands strong data typing and therefore the “simple” dot-notation does not fit in that concept. Thanks for your explanation!

dot notation doesnt mean strong or weak typing at all

Xojo’s JSON simp[ly doent use one of the built in language features to make it possible
And IF they did you would not know the dot notation was in error until runtime

its because value return a variant and not a dictionary.
and because it not know the class it say .Value does not exists.
but you can cast/convert the return into a Dictionary

[code]Var jsonText As String = “{‘town’:{ ‘City’:‘Boston’,‘Team’:‘Red Sox’} }”
jsonText = jsonText.ReplaceAll("’",&u22)

Var mydict As Dictionary
mydict = ParseJSON(jsonText)

MessageBox ( Dictionary(mydict.Value(“town”)).Value(“City”) )[/code]

i think extends would help in your case.

in a modul

[code]Public Function ValueDictionary(Extends obj As Dictionary, name As String) as Dictionary

Return Dictionary(obj.Value(name))

End Function

example to use

MessageBox ( mydict.ValueDictionary("town").Value("City") )