2

我有一个 Java 应用程序从第 3 方 RESTful Web 服务获取以下 JSON:

{
    "widgets":[
        [
            {
                "id":25128,
                "status":"always",
                "uuid":"96f62edd-fa8a-4267-8ffb-14af0d37de26"
            }
        ],
        [
            {
                "id":25200,
                "status":"always",
                "uuid":"78553c9e-398f-495a-8fb8-ada0fb297844"
            }
        ],
        [
            {
                "id":25128,
                "status":"never",
                "uuid":"b1e3deb2-a842-4cba-8272-458d15efb394"
            }
        ]
    ]
}

并尝试将其转换为List<Widget>使用 GSON:

public class Widget {
    @SerializedName("id")
    private Long id;

    @SerializedName("status")
    private String status;

    @SerializedName("uuid")
    private String uuid;

    // Getters & setters, etc.
}

这是我的映射器代码:

String jsonResponse = getJsonFromWebService();
Gson gson = new Gson();
List<Widget> widgets = gson.fromJson(jsonResponse, new TypeToken<List<Widget>>(){}.getType());

当我运行它时,我收到以下错误:

java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

显然,我需要在将 JSON 字符串发送到我的 GSON 映射器代码之前对其进行操作,或者我需要配置 GSON 来处理“意外”的 JSON,但我不确定哪个更容易/更合适。如果我需要“按摩” JSON 字符串,不确定我需要做什么才能让 GSON 很好地使用它。如果我需要配置 GSON,也不知道该做什么。有任何想法吗?提前致谢。

4

2 回答 2

1

问题是您使用单个 JSON 属性“小部件”忽略了根 JSON 对象。尝试将您的数据反序列化到此对象中:

public class WidgetList {
    @SerializedName("widgets")
    private List<List<Widget>> widgets;
}
于 2013-02-28T14:26:23.623 回答
0

将其按摩为以下格式对我有用

[
    {
       'id':25128,
       'status':'always',
       'uuid':'96f62edd-fa8a-4267-8ffb-14af0d37de26'                                                           },       
    {
        'id':25200,
        'status':'always',
        'uuid':'78553c9e-398f-495a-8fb8-ada0fb297844'                                           },
    {   'id':25128,
        'status':'never',
        'uuid':'b1e3deb2-a842-4cba-8272-458d15efb394'   
}
]

如下所示

public class TryMe {

    public static void main(String[] args) {

    Gson gson = new Gson();
        List<Widget> widgets = gson.fromJson(json,
                new TypeToken<List<Widget>>() {
                }.getType());

        System.out.println(widgets);
    }
}

class Widget {
    @SerializedName("id")
    private Long id;

    @SerializedName("status")
    private String status;

    @SerializedName("uuid")
    private String uuid;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    @Override
    public String toString() {
        return "Widget [id=" + id + ", status=" + status + ", uuid=" + uuid
                + "]";
    }   
}

给出以下答复

[Widget [id=25128, status=always, uuid=96f62edd-fa8a-4267-8ffb-14af0d37de26], Widget [id=25200, status=always, uuid=78553c9e-398f-495a-8fb8-ada0fb297844], Widget [id=25128, status=never, uuid=b1e3deb2-a842-4cba-8272-458d15efb394]]
于 2013-02-28T15:01:21.840 回答