JSON

Community Forums/Monkey Talk/JSON

Leon Brown(Posted 2011) [#1]
Does anyone have any information about interpreting JSON data in Monkey? I've tried the demo that comes with Monkey, but couldn't understand it. There was another class that was created for JSON, but the code didn't work.


AdamRedwoods(Posted 2011) [#2]
I looked at the JSON example, and to be honest, wasn't exactly clear how you are suppose to retrieve data out of it. I'd have to look into it more, but I'd rather have the coder explain the thought behind it.

I know you have to do a retr(), but...


Warpy(Posted 2011) [#3]
Really you should try to use native JSON functions when available.

I wrote the example in the bananas folder, and it's unwieldy because Monkey's type system doesn't let you make a JSON-like data structure with any ease. Everything has to be boxed, and because a value can be an object, array or string at any point, you can't make it transparent like with the list/set/stack types in the standard library.

The example at the bottom of json.monkey doesn't really do anything with the loaded data - it just displays it, which is what the .repr method does.

You have to know what data type you expect at each point in your JSON data, so you can call getstringvalue, or getnumbervalue, etc.

Perhaps a better example would be:

Function Main()
	'EXAMPLE
	Local txt$="{~qname~q: ~qChristian~q, ~qage~q: 25, ~qpets~q: [~qdog~q,~qcat~q]}"
	Print txt
	
	Local j:jsondecoder=New jsondecoder(txt)

	j.parse()
	Local jv:jsonobject=jsonobject(j.things.First())
	
	Local name$ = jv.getstringvalue("name")
	Local age = jv.getnumbervalue("age")
	
	Local pets$=""
	For Local js:=Eachin jv.getarrayvalue("pets").values
		If pets.Length pets+=", "
		pets+=jsonstringvalue(js).txt
	Next
	
	Print "Hello "+name
	Print "In two years, you will be "+(age+2)+" years old."
	Print "Pets: "+pets
End


I wouldn't recommend you use this code, but I think someone asked for it during beta, which is why it's in the examples folder.


Leon Brown(Posted 2011) [#4]
Thanks Warpy - that makes a lot more sense now. Are there native JSON features in Monkey?


slenkar(Posted 2011) [#5]
no there are no reflection features at all ;)

but you can load and save strings.


Leon Brown(Posted 2011) [#6]
Thanks guys - I'm experimenting with Warpy's code and am getting the hang of it.


Leon Brown(Posted 2011) [#7]
Hi again. How would I loop through a JSON array of object - so if the JSON looked something like this:

{
  "name": "Christian",
  "age": 25,
  "pets": 
  [
    {
      "type": "dog",
      "sprite": "dog.png"
    },
    {
      "type": "cat",
      "sprite": "cat.png"
    }
  ]
}


As you can see, the pets array is now made of objects describing each pet.


Leon Brown(Posted 2011) [#8]
Hi there. Is anyone able to shed some light on on the above?


Warpy(Posted 2011) [#9]
I suppose I'd better take this one:

	Local txt$ = "{~qdudes~q: [{~qname~q: ~qbob~q, ~qage~q: 1}, {~qname~q: ~qjim~q, ~qage~q: 2}]}"
	Local j:=New jsondecoder(txt)
	j.parse()
	Local v:=j.things.First()
	Local o:=jsonobject(v)
	Local a:=jsonarray(o.getvalue("dudes"))
	For Local v2:=Eachin a.values
		Local o2:=jsonobject(v2)
		Local name$=jsonstringvalue(o2.getvalue("name")).txt
		Local age:=jsonnumbervalue(o2.getvalue("age")).number
		Print "Name: "+name+"~nAge: "+String(age)+"~n"
	Next



Little bit worried you got my age right in your example, Leon...


Leon Brown(Posted 2012) [#10]
Thanks for this Warpy - I'll check this out when I'm working on the code again.

I could claim to be psychic to know your age, but you gave it in the original example ;-).