0

I'm trying to parse Json object to a Java object but i'm having issues with one of its keys. This is the key i'm trying to parse:

"formats":{"application/x-mobipocket-ebook":"http://www.gutenberg.org/ebooks/84.kindle.images",
"text/plain; charset=utf-8":"http://www.gutenberg.org/files/84/84-0.zip",
"text/html; charset=utf-8":"http://www.gutenberg.org/files/84/84-h/84-h.htm",
"application/rdf+xml":"http://www.gutenberg.org/ebooks/84.rdf",
"application/epub+zip":"http://www.gutenberg.org/ebooks/84.epub.images",
"application/zip":"http://www.gutenberg.org/files/84/84-h.zip",
"image/jpeg":"http://www.gutenberg.org/cache/epub/84/pg84.cover.small.jpg"}

So i have the Java class like this:

public class Format {

    @JsonProperty("application/x-mobipocket-ebook")
    private String ebook;

    @JsonProperty("text/plain; charset=utf-8")
    private String textPlain;

    @JsonProperty("text/html; charset=utf-8")
    private String textHtml;

    @JsonProperty("application/rdf+xml")
    private String textXml;

    @JsonProperty("application/epub+zip")
    private String epubZip;

    @JsonProperty("application/zip")
    private String zip;

    @JsonProperty("image/jpeg")
    private String image;

//getters, setters and toString..
}

I'm getting the result of the other keys (its just a json object with name, author, etc) without problems but with this key i'm getting null. How could i get this information properly then? (I've looking for a while now but other answers didn't work)

1
  • 1
    Where is the code that associates "formats" with Format? Commented Jan 2, 2021 at 20:05

1 Answer 1

1

You can use @SerializedName annotation. This annotation indicates the annotated member should be serialized to JSON with the provided name value as its field name.

raw/formats_sample.json

{
   "formats":{
      "application/x-mobipocket-ebook":"http://www.gutenberg.org/ebooks/84.kindle.images",
      "text/plain; charset=utf-8":"http://www.gutenberg.org/files/84/84-0.zip",
      "text/html; charset=utf-8":"http://www.gutenberg.org/files/84/84-h/84-h.htm",
      "application/rdf+xml":"http://www.gutenberg.org/ebooks/84.rdf",
      "application/epub+zip":"http://www.gutenberg.org/ebooks/84.epub.images",
      "application/zip":"http://www.gutenberg.org/files/84/84-h.zip",
      "image/jpeg":"http://www.gutenberg.org/cache/epub/84/pg84.cover.small.jpg"
   }
}

Then in Format class add SerializedName annotation in your attributes

class Format {

    @SerializedName("application/x-mobipocket-ebook")
    private String ebook;

    @SerializedName("text/plain; charset=utf-8")
    private String textPlain;

    @SerializedName("text/html; charset=utf-8")
    private String textHtml;

    @SerializedName("application/rdf+xml")
    private String textXml;

    @SerializedName("application/epub+zip")
    private String epubZip;

    @SerializedName("application/zip")
    private String zip;

    @SerializedName("image/jpeg")
    private String image;
    
//getters, setters and toString..
}

And that's it, have fun!

gson.fromJson(FileUtils.loadFromRaw(context, R.raw.formats_sample), Format::class)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I tried different annotations but this one finally worked with retrofit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.