Json 개체를 Java 개체로 구문 분석하려고하는데 키 중 하나에 문제가 있습니다. 이것이 내가 구문 분석하려는 키입니다.
"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"}
그래서 다음과 같은 Java 클래스가 있습니다.
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..
}
나는 문제없이 다른 키 (이름, 작성자 등이있는 json 객체)의 결과를 얻었지만이 키를 사용하면 null이됩니다. 그러면이 정보를 어떻게 제대로 얻을 수 있습니까? (한동안 찾고 있지만 다른 답변이 작동하지 않았습니다)
@SerializedName 주석을 사용할 수 있습니다. 이 어노테이션은 어노테이션이있는 멤버가 제공된 이름 값을 필드 이름으로 사용하여 JSON으로 직렬화되어야 함을 나타냅니다.
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"
}
}
그런 다음 Format 클래스에서 속성에 SerializedName 주석을 추가하십시오.
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..
}
그리고 그게 다야!
gson.fromJson(FileUtils.loadFromRaw(context, R.raw.formats_sample), Format::class)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다