Probleme with parseJson

Hello
I have some probleme with “ParseJSON”
do you have an idea
Thanck

Var dic As New Dictionary
Var json_in As String
Var json_out As String
Var List() As Variant
Var tx As String

dic.Value ( “Date” ) = “aujourdHui”
dic.Value ( “temps” ) = “fait_beau”
List.Add ( dic )

dic = New Dictionary
dic.Value ( “Date” ) = “demain”
dic.Value ( “temps” ) = “fait_mauvais”
List.Add ( dic )

json_in = GenerateJSON ( List )

Try
json_out = ParseJSON ( json_in )
Catch er
tx = er.Message
End Try

Use JSONItem instead of the Dictionary / ParseJSON system. It’s far easier to use.

// This will hold the array of items
var jsSource as new JSONItem

// First item
var jsItem as new JSONItem
jsItem.Value("Date") = "aujourdHui"
jsItem.Value("temps") = "fait_beau"

// Add to array
jsSource.Add(jsItem)

// Second item
jsItem = new JSONItem
jsItem.Value("Date") = "demain"
jsItem.Value("temps") = "fait_mauvais"

// Add to array
jsSource.Add(jsItem)

// Stringify
var sJSONOut as String = jsSource.ToString

// Reconstruct without error
var jsIn as new JSONItem(sJSONOut)

When posting code, select it and click the Code icon in the post editor toolbar </> so that the forum software doesn’t automatically turn straight quotes into curly quotes.

What is the problem or error you see?

Could it be because List() is type Variant? Try

Var List() As Dictionary

Thank you julia et tim
Solution : Var json_out As variant
bad : Var json_out As String

Merci beaucoup, bonne journée

I always used the Dictionary for dealing with JSON.
I have not tested it, but I read somewhere that JSONitem is slower :thinking:
Isn’t that the case?

It hasn’t been that way for a very long time. In pre-API 2.0 IDEs, yes, that is true.

The namespaced framework (the one where everything was prefixed with Xojo.) introduced native methods for creating and parsing JSON, and that’s where the Dictionary / ParseJSON system comes from. Shortly into API 2.0, the JSONItem class was updated to be a wrapper around that system because of how obtuse and difficult to use the Dictionary system is.

They now do the same thing, while JSONItem handles a lot of the boilerplate and difficult aspects for you, making for cleaner, more obvious, and easier to understand code.

Since Xojo’s target audience is mainly users who prefer easy, I think recommending simple self-documenting code is a better direction.

2 Likes