14

I need help with reading json file to ArrayList.

I have json file:

[
    {
        "name": "Wall",
        "symbol": "#",      
    },
    {
        "name": "Floor",
        "symbol": ".",
    }
]

I have a class:

public class Tile {

    public String name;
    public String symbol;

}

And I have another class with ArrayList:

public class Data {

    public static ArrayList<Tile> tilesData;

    public static void loadData() {
        tilesData = new ArrayList<Tile>();
        Json json = new Json();
        json.fromJson(Tile.class, Gdx.files.internal("data/tiles.json"));
    }

}

I need to fill this ArrayList with data from json file, but I have some problems. I guess the line

json.fromJson(Tile.class, Gdx.files.internal("data/tiles.json"));

is wrong.

When I try to run it there is

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.SerializationException: Error reading file: data/tiles.json

Caused by: com.badlogic.gdx.utils.SerializationException: Unable to convert value to required type: [
{
    name: Wall,
    symbol: #
},
{
    name: Floor,
    symbol: .
}

I have read the libgdx article about json files, but I found it unclear... I don't understand how to fill array. Please, help me with this case!

Pasha
  • 279
  • 3
  • 11

3 Answers3

9

Your json file has ArrayList<Tile> stored in it and you are trying to read it as a Tile.

There are two ways to rectify this.

1) You can encapsulate collection of tiles in another class to simplify serialization.

2) Read as ArrayList and convert type later.

ArrayList<JsonValue> list = json.fromJson(ArrayList.class,
                                          Gdx.files.internal("data/tiles.json"));
for (JsonValue v : list) {
    tilesData.add(json.readValue(Tile.class, v));
}

Hope this helps.

Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45
9

The answer from Tanmay Patil is right but you can save the loop with:

ArrayList<Tile> board = json.fromJson(ArrayList.class, Tile.class, Gdx.files.internal("data/tiles.json"));
Nathan
  • 232
  • 3
  • 10
  • Thank you, it is very usefull addition – Pasha Feb 11 '16 at 18:03
  • Holy crap. I just reduced my original JSON loading code from 10 lines down to 1. LibGDX is pretty sweet. – NielW Mar 09 '16 at 06:27
  • I just came across this post, which is somewhat related to my questions. Instead of reading the json file, is it possible to write the json string ( posted in original question) to the file ? I see class information also printed in the serialized json file, which I don't want. My ques link : http://gamedev.stackexchange.com/questions/125307/unable-to-remove-class-information-in-serialized-json-file-using-libgdx – user2582651 Jul 11 '16 at 08:19
  • 2
    @user2582651 I answered your question, hope it can help you. – Nathan Jul 12 '16 at 11:11
1
ArrayList<Tile> board = json.fromJson(ArrayList.class, Tile.class,
    Gdx.files.internal("data/tiles.json").readString());

2 years and the libgdx has changed a little, so to managed to make it work. Now we have to add .readString(). I was stuck until I figured this out.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Tafa
  • 35
  • 5