I can get items with the following code inspired from Xojo documentation :
Dim d As Xojo.Core.Dictionary
d = Xojo.Data.ParseJSON(JSONDataArea.Text.totext)
Dim consultant As Xojo.Core.Dictionary = d.Value(“consultants”)
Dim consultantnumber As Text = consultant.Value(“idConsultant”)
But I got an issue when my JSON file get more than one “consultant”. Here is a sample I got from an external system:
Hi Fred. In the future, please use the “code” tags in the toolbar to format your code sections. It will help us help you.
In the first case, the “consultants” entry is an object, so your code works because an object translates to a Xojo.Core.Dictionary. But in the second case, it’s an array of objects, translated to Auto(), so you have to make sure you know what you’re dealing with before you can assign it to a variable.
Your code can use Introspection to make the determination, something like this:
dim consultants() as new Xojo.Core.Dictionary
d = Xojo.Data.ParseJSON(JSONDataArea.Text.totext)
dim entry as Auto = d.Value( "consultants" )
if Xojo.Introspection.GetType( entry ) = GetTypeInfo( Xojo.Core.Dictionary ) then
consultants.Append entry
else
dim arr() as Auto = entry
for index as integer = 0 to arr.Ubound
consultants.Append arr( index )
next
end if
[quote=355990:@Kem Tekinay]Hi Fred. In the future, please use the “code” tags in the toolbar to format your code sections. It will help us help you.
In the first case, the “consultants” entry is an object, so your code works because an object translates to a Xojo.Core.Dictionary. But in the second case, it’s an array of objects, translated to Auto(), so you have to make sure you know what you’re dealing with before you can assign it to a variable.
Your code can use Introspection to make the determination, something like this:
dim consultants() as new Xojo.Core.Dictionary
d = Xojo.Data.ParseJSON(JSONDataArea.Text.totext)
dim entry as Auto = d.Value( "consultants" )
if Xojo.Introspection.GetType( entry ) = GetTypeInfo( Xojo.Core.Dictionary ) then
consultants.Append entry
else
dim arr() as Auto = entry
for index as integer = 0 to arr.Ubound
consultants.Append arr( index )
next
end if
I haven’t tested this, but you get the idea.[/quote]
Thanks Kim for your help. ( I will try to pay attention to code display )
I see the idea right but I get a type mismatch error one the first following line :
dim consultants() as new Xojo.Core.Dictionary
Type mismatch error. Expected class Xojo.Code.Dictionary, but got class Xojo.Code.Dictionary ( !!!??? )
Strange error message.
Do u have any idea to solve this new issue?
Unfortunately I don’t know how to use my consultants() dictionary now…
How to get the content of idConsultant value in the second “record” ( which is “32” in sample I have proposed in my first post ) ?
I have made several tries but they generated errors…
Check out http://www.einhugur.com for their very fast and easy to use jsonnode. I tried to convert the old jsonitem to the new frameworks version. Long story short I discovered this plugin jsonnode and life got a lot simpler.
dim consultants() as Xojo.Core.Dictionary
Dim d As Xojo.Core.Dictionary
d = Xojo.Data.ParseJSON(JSONDataArea.Text.totext)
dim entry as Auto = d.Value( "consultants" )
if Xojo.Introspection.GetType( entry ) = GetTypeInfo( Xojo.Core.Dictionary ) then
consultants.Append entry
else
dim arr() as Auto = entry
for index as integer = 0 to arr.Ubound
consultants.Append arr( index )
next
end if
For Each unconsultant as Xojo.Core.DictionaryEntry In consultants
dim consultantDict as Xojo.Core.Dictionary = unconsultant.value
//MsgBox entry.idconsultant
Next
I have added the end “For Each unconsultant as Xojo.Core.DictionaryEntry In consultants etc…” to go through my data stored in consultants dictionary but I got an error on the last next:
“type mismatch error : expected class xojo.core.dictionaryentry but got class xojo.core.dictionary”