Hello,
I am currently facing a problem with Dictionary and json parsing.
When I work on the following JSON file
{
“consultants”:
{
“idConsultant”: “37”,
“numero”: “09874”,
“status”: “ok”
},
“nbConV2”: 2,
“nbConV3”: 0
}
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:
{
“consultants”: [
{
“idConsultant”: “37”,
“numero”: “09874”,
“status”: “ok”
},
{
“idConsultant”: “32”,
“numero”: “09875”,
“status”: “not ok”
}
],
“nbConV2”: 2,
“nbConV3”: 0
}
And in that case the previous code does not work any more unfortunately.
I could I manage x consultant and browse then one by one?
Thanks a lot for your help.
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.
In the second case, d.value(consultants) returns an array of Auto instead of a dictionary.
[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?
Thanks
There shouldn’t be a New in that, sorry.
Thanks Kem for your help. It’s great.
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…
Thanks for help.
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.
Plus its blistering fast
Thanks James but, with help of Kem, I just need now to understand how to get / use data stored in consultants() table… and I can’t succeed so far.
Did u see previous posts James ? ( Anyway I will have a close look to the link u posted )
Thanks to Kem I have the following code :
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”
Any idea to solve this issue?
consultants is now an array of dictionaries. To cycle through them, you would get each element.
for each consultantDict as Xojo.Core.Dictionary in consultants
// do something with consultantDict
next
See
http://developer.xojo.com/arrays
Thanks Kem.
Not possible to reach data directly like
consultantDict(indice).value("dataineed")
?
Lovely. Thanks a lot Kem for your active help.