Retrofit + GSON解析一个对象数组

时间:2015-04-07 20:05:52

标签: android parsing gson retrofit pojo

我收到这个jason作为WS的回复:

[
   [
     "test0",
     "test0"
   ],
   [
     "test1",
     "test1"
   ],
   [
     "test2",
     "test2"
   ],
   [
     "test3",
     "test3"
   ],
   [
     "test4",
     "test4"
   ],
   [
     "test5",
     "test5"
   ]
]

请注意,没有名称 - 值字段,json是字符串数组的数组。 我尝试了几次尝试来解析响应。我尝试使用带有字符串列表的pojo,但我总是遇到同样的错误:

  

retrofit.RetrofitError:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT但在第1行第2行路径为BEGIN_ARRAY $

MyPOJO用于​​改装回调是下一个:

public class VotePollResults {

    private List<PartialResult> fields;

    public List<PartialResult> getFields() {
        return fields;
    }

    public void setFields(List<PartialResult> fields) {
        this.fields = fields;
    }

    public class PartialResult {

        private String description;
        private Integer votes;

        public PartialResult(String description, Integer votes) {
            this.description = description;
            this.votes = votes;
        }

        public String getDescription() {
            return description;
        }

        public Integer getVotes() {
            return votes;
        }

    }

}

我有一个List带有一个自定义对象,即处理该json结构的对象。

2 个答案:

答案 0 :(得分:2)

我解决了这个问题。

我必须在改装时使用它作为回调

Callback<List<List<String>>>

希望这对某人有帮助......

答案 1 :(得分:0)

看起来您正在尝试解析Object而不是Array。如果您回复此代码将起作用:

String[][] items = gson.fromJson(s, String[][].class);
相关问题