当 "parameter" 的名称发生变化时,用 Gson 读取 Json return
Read Json return with Gson when the name of "parameter" change
我正在开发一个应用客户端来连接 Open Library API(https://openlibrary.org/developers/api) to get some information by Json return reading by Gson from google, but I have a problem with one parameter/property that change de name. I sent this request https://openlibrary.org/api/books?bibkeys=ISBN:9788580415544&jscmd=details&format=json
通过 ISBN (9788580415544) 搜索,json return 具有此编号的属性 (ISBN:9788580415544),我无法获取其中的信息。
如果我使用另一个 ISBN 更改请求,我将得到一个 json,另一个名称为 parameter/property
请问,我该怎么做?
return 文件的一部分
{
"ISBN:9788580415544": {
"info_url": "https://openlibrary.org/books/OL26851485M/O_Guia_Definitivo_do_Mochileiro_das_Galáxias_(Em_Portuguese_do_Brasil)",
"bib_key": "ISBN:9788580415544",
"preview_url": "https://openlibrary.org/books/OL26851485M/O_Guia_Definitivo_do_Mochileiro_das_Galáxias_(Em_Portuguese_do_Brasil)",
"thumbnail_url": "https://covers.openlibrary.org/b/id/8540059-S.jpg",
"details": {
"publishers": [
"Arqueiro"
],
"classifications": {},
"key": "/books/OL26851485M",
"source_records": [
"amazon:8580415543"
],
"title": "O Guia Definitivo do Mochileiro das Galáxias (Em Portuguese do Brasil)",
"identifiers": {},
"covers": [
8540059
]
}
}
}
型号Class
public class Objeto {
private ObjetoDetalhe obejtodetalhe;
public ObjetoDetalhe getObejtodetalhe() {
return obejtodetalhe;
}
public void setObejtodetalhe(ObjetoDetalhe obejtodetalhe) {
this.obejtodetalhe = obejtodetalhe;
}
public class ObjetoDetalhe {
private String info_url;
private String bib_key;
private String preview_url;
private String thumbnail_url;
public String getInfo_url() {
return info_url;
}
public void setInfo_url(String info_url) {
this.info_url = info_url;
}
public String getBib_key() {
return bib_key;
}
public void setBib_key(String bib_key) {
this.bib_key = bib_key;
}
public String getPreview_url() {
return preview_url;
}
public void setPreview_url(String preview_url) {
this.preview_url = preview_url;
}
public String getThumbnail_url() {
return thumbnail_url;
}
public void setThumbnail_url(String thumbnail_url) {
this.thumbnail_url = thumbnail_url;
}
}
部分代码
{
Gson gson = new Gson();
Objeto obj = new Objeto();
obj = gson.fromJson(jsonBook.toString(), Objeto.class);
System.out.println(obj.getObejtodetalhe().getInfo_url());
System.out.println(obj.getObejtodetalhe().getThumbnail_url());
}
我建议将顶级对象读作 Map<String, ObjetoDetalhe>
{
"ISBN:9788580415544": {...}
}
"ISBN:9788580415544"
将是键,{...}
将是 ObjetoDetalhe
对象。
public class Objeto {
private map<String, ObjetoDetalhe> obejtodetalhe;
....
请更换型号
我正在开发一个应用客户端来连接 Open Library API(https://openlibrary.org/developers/api) to get some information by Json return reading by Gson from google, but I have a problem with one parameter/property that change de name. I sent this request https://openlibrary.org/api/books?bibkeys=ISBN:9788580415544&jscmd=details&format=json 通过 ISBN (9788580415544) 搜索,json return 具有此编号的属性 (ISBN:9788580415544),我无法获取其中的信息。 如果我使用另一个 ISBN 更改请求,我将得到一个 json,另一个名称为 parameter/property
请问,我该怎么做?
return 文件的一部分
{
"ISBN:9788580415544": {
"info_url": "https://openlibrary.org/books/OL26851485M/O_Guia_Definitivo_do_Mochileiro_das_Galáxias_(Em_Portuguese_do_Brasil)",
"bib_key": "ISBN:9788580415544",
"preview_url": "https://openlibrary.org/books/OL26851485M/O_Guia_Definitivo_do_Mochileiro_das_Galáxias_(Em_Portuguese_do_Brasil)",
"thumbnail_url": "https://covers.openlibrary.org/b/id/8540059-S.jpg",
"details": {
"publishers": [
"Arqueiro"
],
"classifications": {},
"key": "/books/OL26851485M",
"source_records": [
"amazon:8580415543"
],
"title": "O Guia Definitivo do Mochileiro das Galáxias (Em Portuguese do Brasil)",
"identifiers": {},
"covers": [
8540059
]
}
}
}
型号Class
public class Objeto {
private ObjetoDetalhe obejtodetalhe;
public ObjetoDetalhe getObejtodetalhe() {
return obejtodetalhe;
}
public void setObejtodetalhe(ObjetoDetalhe obejtodetalhe) {
this.obejtodetalhe = obejtodetalhe;
}
public class ObjetoDetalhe {
private String info_url;
private String bib_key;
private String preview_url;
private String thumbnail_url;
public String getInfo_url() {
return info_url;
}
public void setInfo_url(String info_url) {
this.info_url = info_url;
}
public String getBib_key() {
return bib_key;
}
public void setBib_key(String bib_key) {
this.bib_key = bib_key;
}
public String getPreview_url() {
return preview_url;
}
public void setPreview_url(String preview_url) {
this.preview_url = preview_url;
}
public String getThumbnail_url() {
return thumbnail_url;
}
public void setThumbnail_url(String thumbnail_url) {
this.thumbnail_url = thumbnail_url;
}
}
部分代码
{
Gson gson = new Gson();
Objeto obj = new Objeto();
obj = gson.fromJson(jsonBook.toString(), Objeto.class);
System.out.println(obj.getObejtodetalhe().getInfo_url());
System.out.println(obj.getObejtodetalhe().getThumbnail_url());
}
我建议将顶级对象读作 Map<String, ObjetoDetalhe>
{
"ISBN:9788580415544": {...}
}
"ISBN:9788580415544"
将是键,{...}
将是 ObjetoDetalhe
对象。
public class Objeto {
private map<String, ObjetoDetalhe> obejtodetalhe;
....
请更换型号