Retrofit + GSON 解析对象数组

Retrofit + GSON parse an Array of objects

我收到此 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: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

改造回调的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 结构。

您似乎在尝试解析对象而不是数组。如果您回应,此代码将起作用:

String[][] items = gson.fromJson(s, String[][].class);

好吧,我解决了这个问题。

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

Callback<List<List<String>>>

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