About Monkey 2 › Forums › Monkey 2 Programming Help › How do you parse JSON files with std.json?
This topic contains 5 replies, has 4 voices, and was last updated by 
 gcmartijn 2 years, 8 months ago.
- 
		AuthorPosts
 - 
		
			
				
August 5, 2016 at 1:15 pm #2788
For fantomX2 I wanna use std.json to parse TileD map files. But I am puzzled who to do that. Can someone lend me a helping hand/code?
August 5, 2016 at 7:01 pm #2802Looks like it works fairly similar to brl.json from MX1… Data members of JsonObject appear to be directly accessible through operator overloading of [] though. Using JsonObject.Load(path) should create the object. Then, you basically either work with the JsonValues directly, or unbox them / bus them to your game’s variables. Again sometimes in MX1 this required direct casting. I don’t know if that situation’s changed now, but if you need to cast, remember that the syntax has changed for MX2 and is now a kinda weird looking operator thing Cast<T>(expression).
Edit: Almost forgot. TMX map format by default uses XML (gross), the json format is an “export” format for Tiled, although its structure is nearly identical to standard TMX, save for changes in the syntax and one or two other things…
August 5, 2016 at 7:21 pm #2805Monkey12345678910111213141516171819Method Load()_name="dark"_fontSize=16_monoFontSize=16Local obj:=JsonObject.Load( "bin/ted2.config.json" )If objIf obj.Contains( "theme" )_name=obj["theme"].ToString()EndifIf obj.Contains( "fontSize" )_fontSize=obj["fontSize"].ToNumber()EndifIf obj.Contains( "monoFontSize" )_monoFontSize=obj["monoFontSize"].ToNumber()EndifEndifthis snippet is from theme.monkey2[20] and is how ted2 parses it’s config file
I consider ted2 one big monkey2 programming example / banana and study it often.
August 5, 2016 at 7:53 pm #2807Thanks Simon. I mixed something up. I didn’t mean Tiled but spritesheets created by TexturePacker.
Anyway, thanks for the hint and example. That should get me started. I Have used a different JSON module before, but wanna got now “standard” and use the BRL implementation to get rid of one 3rd party dependency in my framework.
August 6, 2016 at 12:35 pm #2827I’m having trouble with translating monkey1 code
Monkey12345For Local it:map.Node<String, JsonValue> = Eachin JsonObject( jso.Get("data") ).GetData()Local oclass:JsonObject = JsonObject( it.Value )If it.Key="player" ThenBut how to translate this to monkey2 ?
Monkey123' For Local data:Map.Node<String, JsonValue> = Eachin obj["data"].ToObject()' For Local data:Map.Node<String, JsonValue> = Eachin obj["data"].ToObject().DataIf data.Key = "player"I know that this is working but I need both, the json KEY and Value
Monkey123For Local data:=Eachin obj["data"].ToObject()If data.Key = "player"' but now I don't have the valueThe json file is something like this
{
“w”:2048,
“h”:768,
“p”:200,
“data”:{
“world”:{
“atlas”:”atlas-world1.json”,
“data”:”data-world1.json”
},
“player”:{
“atlas”:”atlas-player.json”,
“data”:”data-player.json”,
“initFrameKey”:”looprechts”,
“initX”:300,
“initY”:712
}
}
}August 7, 2016 at 8:10 am #2834Found the solution after trying
Monkey1234For Local data:=Eachin obj["data"].ToObject()Local obj2:= data.Value.ToObject()If data.Key = "player" - 
		AuthorPosts
 
You must be logged in to reply to this topic.