Question

I have a class and am trying to parse JSON string to instantiate that class. Here is the code and json file:

JSON:

{'walk': ['walk_1','walk_2'], 'jump': ['jump']}

Class:

import java.util.ArrayList;

public class AnimationSheet {

    public ArrayList<String> walk, jump;

}

Function:

Gson json = new Gson();
sheet = json.fromJson(jsonFile, AnimationSheet.class);

But it gives this error:

Exception in thread "LWJGL Application" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1

But it parses successfully if i parse it from a variable as follows:

Gson json = new Gson();
String txt = "{'walk': ['walk_1','walk_2'], 'jump': ['jump']}";
sheet = json.fromJson(txt, AnimationSheet.class);

What is the reason behind this? How can i fix it?

Thank you.

Was it helpful?

Solution

My guess would be that the file doesn't contain the same as your txt String. Looking at the message, it looks like perhaps the file has a stray String at the beginning instead of an object which is what it was expecting. The object in your example String is { or in other words a hash, map or dictionary. If you println the contents of the file to the command line, I expect it doesn't have the exact same characters in it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top