json string to dictionary.

I have a string with json in it.
I want to convert it to a json object and then to a dictionary.
Help?

The new framework can do this for you if you’re interested in that: http://developer.xojo.com/xojo-data$ParseJSON

http://documentation.xojo.com/index.php/JSONItem.Constructor(JSONString_as_String)
You then manipulate the data as if it were a dictionary…

It seems the old framework would allow you to create a JSONItem from a Dictionary, so perhaps you could do the reverse?

Dim dict As Xojo.Core.Dictionary
dict = Xojo.Data.ParseJSON(content.ToText)

{“host”:“name.domain.local”,“source”:“heartbeat”,“event”:{“uptimeMs”:64414110,“lastBootTimeEpochMs”:1503361326865}}

Is raising the exception that the string must have known encoding…

That’s a goofy glitch with ToText.
I avoid it by defining the encoding on the string immediately before using ToText.

It’s not a glitch…

I recall that the default encoding for String is supposed to be UTF8. It’s also glitchy if you define the encoding and manipulate the string before trying to .ToText it.

Dim dict As Xojo.Core.Dictionary Dim aString As String = content.DefineEncoding(Encodings.UTF8) dict = Xojo.Data.ParseJSON(aString.ToText)

Just curious but is Dictionary and Dim dict As Xojo.Core.Dictionary interchangable?
i.e. can I pass an Xojo.Core.Dictionary to a method that is expecting a Dictionary?

For the most part, try to work in either the new framework or the old framework as most (all?) of the new classes are not toll-free to the old framework classes…

[code]dim d as dictionary 'old framework

dim d as xojo.core.dictionary 'new framework

using xojo.core
dim d as dictionary 'new framework[/code]

is there a place where the using statement belongs in a whole project?

Do I itterate through this object the exact same way as I do for the old dictionary?

Lots of sample code in the documentation, but some times finding the right thing is the hard part…

http://developer.xojo.com/xojo-core-dictionaryentry

Each DictionaryEntry in the loop has a .key and .value property…

In traversing the json one of the values itself could be another dictionary.

In the iterator how can you tell if the Value’s type is a xojo.Core.Dictionary and recurse?

Sub parse(d as Xojo.Core.Dictionary) For Each entry As Xojo.Core.DictionaryEntry In d if entry.value.isa(xojo.Core.Dictionary then // ? parse(entry.Value) end if Next End Sub

You can insert a using clause however the scope is private & unchangeable so it is only class or module wide.

how do I tell what the class of an object is?

// This is probably not valid… but do you know what I mean?
entry.value.isa(xojo.Core.Dictionary)

I didn’t test, but if (entry.value IsA Xojo.Core.Dictionary) then

The IsA line throws an type mismatch exception in the debugger, but clearly the object is a Xojo.Core.Dictionary.

[code]Function ToString(d as Xojo.Core.Dictionary, level as integer=0) As String
dim s as string=""
dim tabs as string=""
dim i as integer
dim entry as xojo.Core.DictionaryEntry

for i=0 to level-1
tabs = tabs + chr(9)
next

// Type Mismatch Exception Expected Object but got Int64
For Each entry In d
if entry.Value isa Xojo.Core.Dictionary then
s=s+ToString(entry.Value, level+1)
else
s = s + tabs + entry.Key + “=” + entry.Value + EndOfLine.UNIX
end if
Next

return s
End Function[/code]