简体   繁体   中英

LibGDX: Cannot load a json file from assets folder

I have a json file with data for all the tiles in my game that I store in the assets folder. I try to access and parse it using TileList dataList = json.fromJson(TileList.class, Gdx.file.internal("map-elements/tiles/tiles.json")) . This works fine for the desktop version but on the html version, after converting with gwt, I get these errors:

GwtApplication: exception: Error reading file: map-elements/tiles/tiles.json
Error reading file: map-elements/tiles/tiles.json
Couldn't find Type for class 'net.vediogames.archipelo.world.tiles.TileList'

TileList is a simple object that contains an array of TileData which can then be converted into Tile objects. I did it this way to make the json parsing easy.

The solution to the json error is simple. Instead of passing the FileHandle into the json parser, pass the string from the file like this:

TileList dataList = json.fromJson(TileList.class, Gdx.file.internal("map-elements/tiles/tiles.json").readString());

In the end, all I needed to do to solve that issue is add .readString() . As for the Couldn't find Type for class 'net.vediogames.archipelo.world.tiles.TileList' error, I also found a solution but it is more complicated.

JavaScript handles class references differently than Java. So I was not able to use TileList.class without first registering it so LibGDX can generate a Reflection. What I needed to do was add this line into my *.gwt.xml files:

<extend-configuration-property name="gdx.reflect.include" value="net.vediogames.archipelo.world.tiles.TileList" />

If you want a full tutorial about how reflection works and how to include packages or exclude, please view the official LibGDX tutorial here .

Your solution was not working for me, I still got the same error. After some hours of testing I got it to work, using your suggestions and by using the ClassReflection instead of referencing the class itself. Your example:

TileList dataList = json.fromJson(TileList.class, Gdx.file.internal("map-elements/tiles/tiles.json").readString());

looks in my working code like:

TileList dataList = (TileList) json.fromJson(ClassReflection.forName(TileList.class.getName()), Gdx.file.internal("map-elements/tiles/tiles.json").readString());

This is quite a pain in the a.. but I'm glad it is finally working now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM