Receiving A JSON Object Instead of Array

I’ve watched Paul’s video on HTTPSocket and receiving a JSON array from a REST-API, but what if I just want to return something very simple, like just a JSON object? Something like: { “1”,“2”,“3”} or {“result”:“true”}.

I can’t seem to make Xojo recognize that as a JSON object. Do I have to receive a JSON array?

Your first example is not valid JSON. An object just be in the form "key" : value, so only the second one will work.

dim d as Xojo.Core.Dictionary = Xojo.Date.ParseJSON( jsonObjectText )

// or

dim j as new JSONItem( jsonObjectString )

[quote=393011:@Kem Tekinay]Your first example is not valid JSON. An object just be in the form "key" : value, so only the second one will work.

[code]
dim d as Xojo.Core.Dictionary = Xojo.Date.ParseJSON( jsonObjectText )

// or

dim j as new JSONItem( jsonObjectString )
[/code][/quote]

Is that .Date. part right? What if it’s not a date?

I will attempt this again, but IIRC ParseJSON() was rejecting a JSON object (like {“valid”:“true”}).

Also, could I return something like [ “1”,“2”,“3” ] as a valid array?

And let’s say I parsed {“result”:“true”} successfully. How would I retrieve the “result” value?

Thank you!

No, that should have said Data. Sometimes you have to infer or correct things with free examples.

You would have to turn it into valid JSON (as Kem noted "key": value) so something like {"NumbersArray": [1, 2, 3]} would work. You should just use the methods that build and deconstruct JSON rather than manually handling it (on both ends).

This depends on your framework of choice.
Namespaced Framework: http://developer.xojo.com/xojo-core-dictionary$GetIterator
Classic Framework: http://documentation.xojo.com/index.php/JSONItem.Names

Sorry, typo, should be Xojo.Data.ParseJSON.

Yes, that is a valid array.

Once you parse an object into a Dictionary, you would use it as a normal, albeit case-sensitive, Dictionary.

dim d as Xojo.Core.Dictionary = Xojo.Data.ParseJSON( "{""result"":""true""}" )
if d.Value( "result" ) = "true" then ...

This might help:

https://forum.xojo.com/46409-tip-reading-json

Yes, I realized it should have been “data” after I posted my question.

This is telling me “Bad JSON.” I have no idea why… :-/

[code]Dim jsonData as text

jsonData = “{”“result”":"“true”"}"

dim items() as Auto
try
items=Xojo.Data.ParseJSON(jsonData)
msgbox “Good JSON:’” + jsonData + “’”
catch
msgbox “Bad JSON:’” + jsonData + “’”
end try
[/code]

Because that json represents an object, or Dictionary, but you are attempting to assign it to an array.

Ahhh… “items()” means array. OK. Thank you!