Importing JSON (Tiled Map)

Hey guys, I’ve got a tiled map in Json format and I see there is a Haxe API for importing these things, however I cant figure it out. I simply want to read in a text file and get access to the “data”.

Heres what I’ve attempted that doesnt work:
var mp = sys.io.File.getContent(‘map/tiles.json’);
var myObject = haxe.Json.parse(mp);

Which throws me this error:
> Called from ? line 1

Called from ApplicationMain.hx line 139
Called from ApplicationMain.hx line 26
Called from lime/app/Preloader.hx line 58
Called from openfl/display/Preloader.hx line 93
Called from NMEPreloader.hx line 107
Called from openfl/events/EventDispatcher.hx line 95
Called from openfl/display/DisplayObject.hx line 270
Called from a C function
Called from openfl/events/EventDispatcher.hx line 237
Called from openfl/display/Preloader.hx line 129
Called from a C function
Called from lime/app/Preloader.hx line 242
Called from lime/app/Event.hx line 141
Called from ApplicationMain.hx line 83
Called from ApplicationMain.hx line 165
Called from a C function
Called from Main.hx line 105
Called from openfl/display/DisplayObjectContainer.hx line 59
Called from openfl/display/DisplayObjectContainer.hx line 787
Called from openfl/display/DisplayObject.hx line 270
Called from a C function
Called from openfl/events/EventDispatcher.hx line 237
Called from Main.hx line 96
Called from Main.hx line 42
Called from /usr/lib/haxe/std/neko/_std/sys/io/File.hx line 30
Uncaught exception - [file_contents,map/tiles.json]

I’ve use the “exists” function,exists(“img/tiles.png”)==true), and can see the file exists.

Try adding the file to your “Assets” directory, then Assets.getText ("assets/map/tiles.json") instead of File.getContent, that should work better cross-platform :slight_smile:

Wow ya, that was it. Sorry for asking such a newbie question, missing the assets folder in my directory was the issue, thanks very much for the help.

No problem! The Assets class is helpful because it works embedded/non-embedded, web, desktop and mobile in a consistent manner (while File.getContent would probably only work on the desktop)

  var mapJson = haxe.Json.parse(sys.io.File.getContent('assets/map/tiles.json'));
  map = new Array<Int>();
  map = mapJson.layers[0].data;

This is my code for pulling Json data. Pulls the tiles.json then dumps it into a map array. In case anyone ever needs it. Tiles.json is made with tiled and exported as Json.

1 Like