I am trying to parse the Json result returned from this url . I want to get title, content and thumbnail value. I tried to make a java class to Map those values .
import java.util.List;
public class Dbpediaresults {
private Results results;
public Results getResults() {
return results;
}
public void setResults(Results results) {
this.results = results;
}
public static class Results {
private List<Bindings> bindings;
public List<Bindings> getBindings() {
return bindings;
}
public void setBindings(List<Bindings> bindings) {
this.bindings = bindings;
}
}
public static class Bindings {
private List<Title> title;
private List<content> content;
private List<Thumbnail> thumbnail;
/**
* @return the title
*/
public List<Title> getTitle() {
return title;
}
public void setTitle(List<Title> title) {
this.title = title;
}
public List<content> getContent() {
return content;
}
public void setContent(List<content> content) {
this.content = content;
}
public List<Thumbnail> getThumbnail() {
return thumbnail;
}
public void setThumbnail(List<Thumbnail> thumbnail) {
this.thumbnail = thumbnail;
}
}
private static class Title {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
private static class content {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
private static class Thumbnail {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}
I tried this to get the result but it isn't working.
Dbpediaresults results = new Gson().fromJson(input, Dbpediaresults.class);
Here input is a String
of the json output returned from URL above.
Is my Dbpediaresults
class is correct to map the title, content and thumbnail value? If not, how can I parse it correctly?
Gson will not break the Binding
group into three separate lists like that. The easiest way to fix this would be to create containing a single List<Binding>
, where Binding
is a class that represents all three of title, content, and thumbnail. It's better to keep these together anyway as multiple parallel List
s are a strong hint that there's a class waiting to be born.
Here's some sample code. In addition to fixing the problem, I made some other minor style and variable name changes (such as class Content
being lowercase in your code, and removing the setters).
public class Dbpediaresults {
private Results results;
public Results getResults() {
return results;
}
public void setResults(Results results) {
this.results = results;
}
public static class Results {
private List<Binding> bindings;
public List<Binding> getBindings() {
return bindings;
}
}
public static class Binding {
private Title title;
private Content content;
private Thumbnail thumbnail;
public Title getTitle() {
return title;
}
public Content getContent() {
return content;
}
public Thumbnail getThumbnail() {
return thumbnail;
}
}
public static class Title {
private String value;
public String getValue() {
return value;
}
}
public static class Content {
private String value;
public String getValue() {
return value;
}
}
public static class Thumbnail {
private String value;
public String getValue() {
return value;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.