deezzel deezzel - 7 months ago 12
Java Question

user.getInfo and GSON parsing in Java

Below is the sample response from 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"
}
],


I am trying to parse this using GSON but don't know how to parse this line:


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


For instance, I use class with two fields:

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;
}
}


But after the parsing I get null in text field because the field in json named as "#text" and not "text".

Can anybody help me please?

Answer

Try using gson annotation @SerializedName.

 @SerializedName("#text")
 String text;
Comments