0

I have the following json string:

{"objKampfEntry":[{"deffSpielerId":"9","kampfId":"7","offSpielerId":"10","rundeCounter":"0","rundenList":[{"deffIsReady":"0","deffMove":"0","deffSpieler":{"ausdauer":"5","eiId":"0","gesichtId":"normal","helm":"0","id":"13","kraft":"6","leben":"12","lebenMax":"50","level":"1","name":"Test","niederlagen":"0","punkte":"1","rang":"1","rustung":"0","siege":"0","timestamp":"1397385595686","waffe":"0"},"done":"0","kampfId":"7","offIsReady":"1","offMove":"0","offSpieler":{"ausdauer":"5","eiId":"0","gesichtId":"normal","helm":"0","id":"14","kraft":"6","leben":"12","lebenMax":"50","level":"1","name":"Test2","niederlagen":"0","punkte":"1","rang":"1","rustung":"0","siege":"0","timestamp":"1397385841118","waffe":"0"},"rundeNr":"0","winner":"0"}]}]}

If I use

Gson gson = new Gson();
Type listOfKampfEntry = new TypeToken<List<ObjKampfEntry>>(){}.getType();
ArrayList<ObjKampfEntry> list = gson.fromJson(json, listOfKampfEntry);

I get this error:

04-13 12:45:48.706: W/System.err(4411): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
04-13 12:45:48.706: W/System.err(4411):     at com.google.gson.Gson.fromJson(Gson.java:815)
04-13 12:45:48.706: W/System.err(4411):     at com.google.gson.Gson.fromJson(Gson.java:768)
04-13 12:45:48.706: W/System.err(4411):     at com.google.gson.Gson.fromJson(Gson.java:717)

So I have an array of objKampfEntry and inside an array of rundenList. What am I doing wrong?

My ObjKampfEntry class:

public class ObjKampfEntry {

    private long kampfId;
    private long offSpielerId;
    private long deffSpielerId;

    private int rundeCounter;

    private ArrayList<Runde> rundenList = new ArrayList<Runde>();
    //constructor and getter/setter

}

And the runde object

public class Runde {

    private long kampfId;
    private long winner;
    private int rundeNr;
    private int offIsReady;
    private int deffIsReady;
    private int done;
    private Spieler offSpieler;
    private Spieler deffSpieler;
    private int offMove;
    private int deffMove;
...

Here the Spieler object

public abstract class ACharakter {

    protected long id;
    protected String name;
    protected int leben;
    protected int lebenMax;
    protected int kraft;
    protected int ausdauer;

public class Spieler extends ACharakter {

    private int rang;
    private int level;
    private int punkte;
    private long timestamp;
    private int waffe;
    private int rustung;
    private int helm;
    private int siege;
    private int niederlagen;
    private int eiId;
    private String gesichtId;
1
  • Post Spieler.java code Commented Apr 13, 2014 at 11:05

1 Answer 1

3

Your input is not a JSON array of ObjKampfEntry objects. Instead, it is a JSON object with one key/value pair whose value is a JSON array of ObjKampfEntry objects.

The "correct" input according to your scheme would be [{"deffSpielerId":"9","kampfId":"7",...}], as that will indeed map to a List<ObjKampfEntry>. If you can't change the input, then you'll need another class holding one field named objKampfEntry of type List<ObjKampfEntry>:

public class ObjKampfEntries {

    private ArrayList<ObjKampfEntry> objKampfEntry = new ArrayList<ObjKampfEntry>();

}

ObjKampfEntries entries = gson.fromJson(json, ObjKampfEntries.class);
List<ObjKampfEntry> list = entries.objKampfEntry;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.