About Monkey 2 › Forums › Monkey 2 Programming Help › Json question
Tagged: json
This topic contains 5 replies, has 3 voices, and was last updated by 
 Ethernaut
 2 years, 3 months ago.
- 
		AuthorPosts
 - 
		
			
				
January 11, 2017 at 8:05 am #6406
Consider this json file:
JavaScript1234567891011121314151617181920212223{ "animationClips": {"right": {"loop":true,"rate":12,"frames":[0,1,2,3]},"left": {"loop":true,"rate":12,"frames":[4,5,6,7]},"up": {"loop":true,"rate":12,"frames":[8,9,10,11]},"down": {"loop":true,"rate":12,"frames":[12,13,14,15]}}}And this M2 code to read it:
Monkey123456789101112131415161718192021222324252627282930313233#Import "<std>"#Import "test.json"Using std..Function Main()Local data := JsonObject.Load( "asset::test.json" )If dataLocal clips := data.GetObject( "animationClips" )If clipsFor Local c := Eachin clips.ToObject()Local loop := Cast<JsonObject>( c.Value ).GetBool( "loop" )Local rate := Cast<JsonObject>( c.Value ).GetNumber( "rate" )Local frames := Cast<JsonObject>( c.Value ).GetArray( "frames" )Print " ~nanim:" + c.KeyIf loop Print "True"If rate Print rateIf framesLocal text := ""For Local n := Eachin framestext += n.ToNumber() + " "NextPrint textEndNextEndEndEndI want to iterate through each of the animation clips in the json file and print their values, just for this test.
Is there a cleaner way to access the iterator? Can I get rid of all that casting and get the correct type (JsonObject) out of the gate? JsonObject.All is not working for me, I get the “Iterator has no ‘AtEnd’ property” error.
January 11, 2017 at 9:04 am #6408can’t see most of your code, not sure what you’re trying to improve…??
January 11, 2017 at 9:25 am #64091.I’m trying to get rid of the casting. Is there a way to iterate the JsonObjects directly, instead of JsonValues? Which led me to…
2.I was wondering if there’s something wrong with JsonObject.All. I get an error when I try to use it instead of JsonObject.ToObject() (which returns a map)
Thanks.
January 11, 2017 at 12:46 pm #6411I use ToString/ToNumber etc. Here’s a snippet from a json loader I’m using if it helps:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354json = json.Load(file)directory = ExtractDir(file)If Not json RuntimeError("Couldn't load config for " + file)If json["width"] width = json["width"].ToNumber()If json["height"] height = json["height"].ToNumber()If json["frames"] frames = json["frames"].ToNumber()If json["statictile"] statictile = json["statictile"].ToNumber()If json["collisions"] And json["collisions"].IsArraycollisionframe = New tlCollisionInfo[json["collisions"].ToArray().Length]Local index:Int = 0Local collisions:=json["collisions"].ToArray()collisionframe = New tlCollisionInfo[collisions.Length]For Local collisionobj:=Eachin collisionsLocal collision:=collisionobj.ToObject()If collision["type"]Select collision["type"].ToString()Case "poly"collisionframe[index] = New tlCollisionInfocollisionframe[index].collisiontype = tlPOLY_COLLISIONcollisionframe[index].x = collision["handlex"].ToNumber()collisionframe[index].y = collision["handley"].ToNumber()collisionframe[index].verts = JsonFloatArray(collision["verts"].ToArray())Case "box"collisionframe[index] = New tlCollisionInfocollisionframe[index].collisiontype = tlBOX_COLLISIONcollisionframe[index].x = collision["handlex"].ToNumber()collisionframe[index].y = collision["handley"].ToNumber()collisionframe[index].width = collision["width"].ToNumber()collisionframe[index].height = collision["height"].ToNumber()Case "circle"collisionframe[index] = New tlCollisionInfocollisionframe[index].collisiontype = tlCIRCLE_COLLISIONcollisionframe[index].x = collision["handlex"].ToNumber()collisionframe[index].y = collision["handley"].ToNumber()collisionframe[index].radius = collision["radius"].ToNumber()EndElseRuntimeError("Error loading resource config")End Ifindex+=1NextEnd IfIf json["files"] And json["files"].IsArrayfiles = New String[json["files"].ToArray().Length]Local tmpfiles:=json["files"].ToArray()Local i:Int = 0For Local file:=Eachin tmpfilesfiles[i] = file.ToString()i+=1NextEnd IfAnd the json file for reference:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384{"width":32,"height":32,"frames":16,"files":["solidblock_01.png","solidblock_02.png","solidblock_03.png","solidblock_04.png","solidblock_05.png","solidblock_06.png","solidblock_07.png","solidblock_08.png","solidblock_09.png","solidblock_10.png","solidblock_11.png","solidblock_12.png","solidblock_13.png","solidblock_14.png","solidblock_15.png","solidblock_16.png"],"statictile":1,"collisions":[{"type":"poly","handlex":10.5,"handley":10.5,"verts":[32,32,32,16,16,32]},{"type":"poly","handlex":-10.5,"handley":10.5,"verts":[16,32,0,16,0,32]},{"type":"poly","handlex":10.5,"handley":-10.5,"verts":[32,0,16,0,32,16]},{"type":"poly","handlex":-10.5,"handley":-10.5,"verts":[16,0,0,0,0,16]},{"type":"poly","handlex":0,"handley":0,"verts":[0,16,0,32,32,32,32,0,16,0]},{"type":"poly","handlex":0,"handley":0,"verts":[0,0,0,32,32,32,32,16,16,0]},{"type":"poly","handlex":0,"handley":0,"verts":[0,0,0,32,16,32,32,16,32,0]},{"type":"poly","handlex":0,"handley":0,"verts":[0,0,0,16,16,32,32,32,32,0]},{"type":"box","handlex":8,"handley":0,"width":16,"height":32},{"type":"box","handlex":0,"handley":-8,"width":32,"height":16},{"type":"box","handlex":-8,"handley":0,"width":16,"height":32},{"type":"box","handlex":0,"handley":8,"width":32,"height":16},{"type":"poly","handlex":0,"handley":0,"verts":[0,0,0,16,16,32,32,32,32,16,16,0]},{"type":"poly","handlex":0,"handley":0,"verts":[0,16,0,32,16,32,32,16,32,0,16,0]},{"type":"box","handlex":0,"handley":0,"width":32,"height":32},{"type":"box","handlex":0,"handley":0,"width":32,"height":32}]}January 11, 2017 at 3:40 pm #6413[/crayon]Monkey123456[crayon-5cb9c1efa79dd366168617 inline="true" ]If json["width"] width = json["width"].ToNumber()If json["height"] height = json["height"].ToNumber()If json["frames"] frames = json["frames"].ToNumber()I wonder if these couldn’t be done with reflection, ie the jason property name provides the variable name thats looked up and used via reflection, but mind you this could potentially cause a security issue…
January 11, 2017 at 7:00 pm #6419Thanks, that is helpful. This looks a bit more neat.
Monkey12345678910111213141516171819202122#Import "<std>"#Import "test.json"Using std..Function Main()Local data := JsonObject.Load( "asset::test.json" )If dataLocal clips := data.GetObject( "animationClips" )If clipsFor Local c := Eachin clips.ToObject()Local obj := c.Value.ToObject()Local loop := obj[ "loop" ].ToBool()Local rate := obj[ "rate" ].ToNumber()Local frames := obj[ "frames" ].ToArray()NextEndEndEndI had t add the line “Local obj := c.Value.ToObject()” which essentially casts the JsonValue as an object, so functionally it’s not that different, but it’s easier to read.
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.