Dictionary help

Any help with bellow problems guys?
The environment is for IOS
From searching i see to parse json on “IOS” is only Dictonary
From the help of member Kem T whan i ask on previous conversation i try to parse my json.
I have the bellow Json data. and i have 2 probelms.
My simple code is this bellow and i want to see the keys and the values from json to the field (textarea1)
On line 2 i have the break so i go to debugger to check the tData.
On tData

then i click to Xojo.core.dictionary and i get the bellow
Is this right here? is only for info ? Or i must see the Keys and values?

And click to contents again and i see the key and the value

On line 6 i get always TypeMismatchException

Sub Action() dim tData as Xojo.Core.Dictionary = Xojo.Data.ParseJSON (tjson.text) break for each entry as Xojo.Core.DictionaryEntry in tData TextArea1.Text = TextArea1.Text + " " + entry.Key + " " + entry.Value <--- TypeMismatchException // entry has both key and value so do something with them next End Sub
and the Second problem is that the Data in the json is 90 lines (clean data No returns) and the dictionary is only 56

A value in JSON can be any of: Null, boolean, number, string, object, or array. When you convert these to Xojo, you get a Dictionary (in your case) with keys (“current_observation”, “response”) whose values are other Dictionaries. You can’t just go through and list each item, you will have to test it first. Something like this:

Function ContentsOfDictionary (d As Xojo.Core.Dictionary, level As Integer = 0) As Text
  const kEOL = &u0A

  dim indent as text
  for i as integer = 1 to level
    indent = indent + " "
  next

  dim t as text
  for each entry as Xojo.Core.DictionaryEntry in dict
    t = t + indent + entry.Key + " "
    if Xojo.Introspection.GetType( entry.Value ) is nil then
      t = t + "nil" + kEOL
    else
      select case Xojo.Introspection.GetType( entry.Value ).Name
      case "Boolean"
        t = t + if( entry.Value, "true", "false ) + kEOL
      case "Text"
        t = t + entry.Value + kEOL
      case "Int32", "Int64"
        //
        // You get the idea
        //
      case "Dictionary"
        t = t  + kEOL + ContentsOfDictionary( entry.Value, level + 1 )
      end select
  end if

  return t
End Function

I haven’t tested this code, but it should be enough to give you an idea of how to deal with it.

Thank you Kem.
But still i don’t understand how the Json parse in XOJO work.I know json have (Null, boolean, number, string, object).
The meaning of parse is to get the Each Line and make ARRAY with the key (or Keys) names and results from keys is the child (1]+value) etc…

first key [Response] The Data [1]Team + value [2]Team + value [3]Team + value Second key [AnotherName] [1]Player + value [2]Team + value [3]Team + value
i am reading more than 10 hours to understand the meaning of parse in XOJO on forum + Google and the only i have get until now is to confuse me more and more.
Is very frustrate 10 hours just to get some data to field.
I want the key Name label1 =My title and label2 = The Value
From here i get the json data MyJson
Can anyone give me a working example with this data parsed on IOS?
Really appreciate any help.

Your problem is, the value for each key is not a string, it’s another Dictionary.

If the JSON string looked something like this, it would be easy:

{
  "key1" : "data1",
  "key2" : "data2"
}

Each key has a string value so you can cycle through the keys and extract the strings. But your entries are other Dictionaries, so they look more like this this:

{
  "key1" : 
    {
    "subkey1" : "subdata1"
    }
}

So key1 in the example above leads to another Dictionary, so you can’t add it to a text field directly. That’s why my code will help since it will “walk” through the Dictionary and create the text as it goes.

Thank you Ken i will give one more try again.
but the above code give me errors if i put it on button or method.
He give me error on the end function

That was meant to show a function you could add to your project in a module or view that would perform that function. The code is incomplete so you would have to add more to it. It’s meant just to get you started.

To be clear, you wouldn’t include the first line (Function …) or the last line (End Function). Instead, you’d add a method called ContentsOfDictionary, give it the parameters d As Xojo.Core.Dictionary, level As Integer = 0, and have it return Text.

Ok then i start all over again :wink:

i have make the method in the view,fixed and no errors,but how i call the method ContentsOfDictionary from button action.?
My Test file

In the button action:

TextField1.Text = ContentsOfDictionary( jsonText )