1

I was looking at some examples but I couldn't find anything solving my problem. I guess problem is trivial but I can't what I am doing wrong. I have such looking simple JSON:

{
  "value" : 120,
  "objects" : [
    {
      "object_id" : 1,
      "description" : "First object",
      "flag" : false
    },
    {
     "object_id" : 2,
      "description" : "Second object",
      "flag" : true
    }
  ]
}

I created a model for it:

GeneralResponse:

public class GeneralResponse {

    public int value;
    public List<NestedObject> objects;

}

NestedObject:

public class NestedObject {

    public int object_id;
    public String description;
    public boolean flag;

}

And I try to parse it like that:

GeneralResponse response = new Gson().fromJson(myJSONstring, GeneralResponse.class);

And I get an error:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 4 column 6 path $.objects[0]
Yu Zhang
  • 2,037
  • 6
  • 28
  • 42
F1sher
  • 7,140
  • 11
  • 48
  • 85
  • 2
    Which version of GSON are you using? I tested with 2.4 and it works as expected. – Tunaki Nov 02 '15 at 19:53
  • Hi, Fisher @F1sher I am not sure this would help you, why we need to re write for Parsing JSON ? why can't we use any import any json module and can't use it . http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – Shankar Nov 02 '15 at 19:54
  • This thread might help you : http://stackoverflow.com/questions/9598707/gson-throwing-expected-begin-object-but-was-begin-array – sr09 Nov 02 '15 at 20:09
  • 1
    I apologise everyone. I didn't notice that my NestedObject is an enum with fields and this cause error. Thank you for looking into my matter anyway... – F1sher Nov 02 '15 at 20:41
  • @F1sher please add this information as answer to this question – MariuszS Nov 02 '15 at 21:03

1 Answers1

0

There is an error in question code, after the author:

NestedObject is an enum with fields and this cause error.

MariuszS
  • 30,646
  • 12
  • 114
  • 155