【问题标题】:user.getInfo and GSON parsing in JavaJava 中的 user.getInfo 和 GSON 解析
【发布时间】:2014-08-19 19:36:10
【问题描述】:

以下是来自 last.fm api 的示例响应

"image": [
{
"#text": "http://userserve-ak.last.fm/serve/34/86765245.jpg";,
"size": "small"
},
{
"#text": "http://userserve-ak.last.fm/serve/64/86765245.jpg";,
"size": "medium"
},
{
"#text": "http://userserve-ak.last.fm/serve/126/86765245.jpg";,
"size": "large"
},
{
"#text": "http://userserve-ak.last.fm/serve/252/86765245.jpg";,
"size": "extralarge"
}
],

我正在尝试使用 GSON 解析它,但不知道如何解析这一行:

"#text": "http://userserve-ak.last.fm/serve/126/86765245.jpg"

例如,我使用带有两个字段的类:

public static final class Image {
    private String text;
    private String size;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }
}

但解析后我在文本字段中得到 null,因为 json 中的字段名为“#text”而不是“text”。

有人可以帮帮我吗?

【问题讨论】:

标签: java api gson last.fm


【解决方案1】:

尝试使用gson annotation@SerializedName。

 @SerializedName("#text")
 String text;

【讨论】:

    猜你喜欢
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    相关资源
    最近更新 更多