如何使用嵌套数组将JSON解析为object

时间:2015-11-02 19:46:09

标签: java json parsing

我在看一些例子,但我找不到解决问题的方法。我想问题是微不足道的,但我不能说我做错了。我看起来很简单JSON:

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

我为它创建了一个模型:

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;

}

我尝试解析它:

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

我收到错误:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 4 column 6 path $.objects[0]

1 个答案:

答案 0 :(得分:0)

问题代码中有错误,在作者之后:

  

NestedObject是一个包含字段的枚举,这会导致错误。