nested JSON with brl.json?

Monkey Forums/Monkey Programming/nested JSON with brl.json?

jondecker76(Posted 2014) [#1]
Hello

I'm doing some tests with the brl.json module.
I'm finding simple JSON objects simple to deal with..

However, it's not clear on how to handle an array of objects. Take the following JSON for example:
{"success":true, "msg":"Success", "data":[{"key":1, "name":"First key","user":"User123"},{"key":2,"name":"Second key","user":"UserABC"},{"key":3,"name":"Third key","user":"User XYZ"}]}

I'm a little stuck with how to deal with the data array (which is an array of objects)

Example snippet:
Local jstring:String=LoadString("test.json")
Local js:JsonOvject = New JsonObject(jstring)

' Parse the success flag
Local success:Bool=js.GetBool("success")

'Parse the message
Local msg:String=js.GetString("msg")

' How to do the array of objects??
Local data:=JsonArray(js.Get("data"))
For Local i:=0 Until data.Length()
  ' Hmm..  What to do now to get data->key, data->name and data->user?
next


Thanks


CopperCircle(Posted 2014) [#2]
Hi, I use brl.json allot, takes a little getting used to but works very well. Here ya go:




jondecker76(Posted 2014) [#3]
Thanks a lot

Definitely much different than json_decode/encode in PHP! It'll definitely take a little getting used to, but very workable.


mmitchell(Posted 2014) [#4]
EDIT: nevermind, somehow I didn't see the replies before signing in... not sure how that happened.

I was having trouble trying to parse JSON too, your post helped a lot so I thought I'd provide the missing piece of the puzzle. :)

For Local i:=0 Until data.Length()
	Local jsb := JsonObject(js.Get(i))
	Local user = jsb.GetString("user")
next



Monking(Posted 2014) [#5]
Am I the only have this error when compiling above codes ?
I have this error:

modules/brl/json.monkey<168> : Error : Identifier 'Get' not found.


which is this line:
Local o:JsonObject = JsonObject(data.Get(d))
or this line:
Local jsb := JsonObject(js.Get(i))

Is it my json.monkey has error or what?
Thanks.


ImmutableOctet(SKNG)(Posted 2014) [#6]
You're using 'Get' on a class that doesn't have it. Move your call to 'Get' to after the dynamic-cast. (See below for details)

Something like this:


This is a bug in your own code (And possibly mmitchell's code, but that's a bit ambiguous to determine), not Monkey's 'brl.json' module.

Okay, so what's actually going on here? Basically, you seem to be misinterpreting the code found in this thread. CopperCircle's code already had a valid 'JsonArray', which he then used its specific 'Get' method in order to get the internal data (A 'JsonValue' object, or something based on 'JsonValue'). Your code seems to be using an invalid type to use 'Get' with. This is likely an un-casted object which was being represented by 'JsonValue' (Which 'JsonArray' produces), an incapable class for this situation. So, since we don't know the type of the object on compile-time, that cast I posted is needed in order to check if it's a 'JsonObject' (Though, if it's always a 'JsonObject', you could remove the check and do it in-line). Monkey's statically typed; this is with good reason. Many languages (Especially the ones compiled to by Monkey) go this route. Here's a small list: C, C++, C# (With some exceptions), Java (With some exceptions), Objective C, the list goes on. So dynamic casting is needed in order to read the runtime type-data, which allows us to know what type each object is. Basically, dynamic casting (Which in the case of Monkey is most explicit object-related casting) is a way for us to write code statically which needs to be able to deal with dynamic concepts like objects and polymorphism.

Here's an example of in-line casting (Not exactly safe):
Local info:= JsonObject(data).Get(d)



Monking(Posted 2014) [#7]
The error of mine is because I'm using older monkey version v75d, the error fix after download latest version. Thanks.