Hi all!
I have a JSONItem object (json), which has some childs. I want to get the 1st child (which is a JSONItem itself), but I’m not sure how to do that.
At the moment I’m using this approach:
dim json_parse as new jsonitem
dim artist as string
...
json_parse.Load(json.Value(json.Name(1)).StringValue) //I'm not sure how to do this exactly, it gives me the exception here.
artist = json_parse.Value("name")
MsgBox artist
But it’s throwing me a TypeMismatchException. I’m sure that’s a dumb question, but unfortunately I’m not that experienced with Xojo.
JSONItem works either as an array or as a Dictionary, depending on it’s values. If it’s an array and you want the first index, you can simply do this:
dim child as JSONItem = json( 0 ) // 0 is the first element in the array
If the object is as a Dictionary and you’re looking for the “artist” child, do this:
dim child as JSONItem = json.Value( "artist" )
Thank you very much, I resolved.
I’m a little confused by this as well.
I’ve got a json config file that has two root elements.
the first is a dictionary and the second is an array.
dim MyJSon as string = "{
"PLUDictionary" : {
"Apple" : "111",
"Orange" : "112"
},
"ShoppingList" : [
"Apple",
"Banana"
]
}"
ParseJson(JSONItem(MyJSon))
[code]
Sub ParseJson(json As JSONItem)
dim c as integer = json.Count // I think this is the number of arrays or dictionaries in the json.
For i As Integer = 0 to c-1
if not json.IsArray then
for j as integer = 0 to ???
next
else
iterate through the json keys?
end if
Next i
End Sub[/code]
I’m trying to figure out how I can iterate through this JSON recursively.