将 JSONObject 转换为对象列表
converting JSONObject to Object list
我想将 JSON对象(数组)转换为对象列表。
因为我是 java 的新手,所以遇到了一些问题。
JSON:
"products": [
{
"pid": "0",
"name": "Product Na",
"kategorie": "Category",
"beschreibung": "Description",
"bild": "http:\/\/arsdecora.net\/wp-content\/uploads\/2015\/04\/B1696.jpg",
"preis": "0"
},
{
"pid": "1160",
"name": "Beispiel B",
"kategorie": null,
"beschreibung": null,
"bild": "http:\/\/arsdecora.net\/wp-content\/uploads\/2015\/04\/B1696.jpg",
"preis": "0"
},
产品class:
public class Produkt {
public String id;
public String name;
public String categorie;
public String description;
public String image;
public double price;
}
我已经用 gson 尝试了几件事,但最终都没有奏效。
我不需要工作代码,只是关于如何通过标签反序列化 JSON 的提示。
希望你能帮助我。提前致谢!
尝试创建一个包含产品列表的 class。这是一个完整的例子:
在您的 json 数据周围添加方括号,如下所示:
{
"products": [
{
"pid": "0",
"name": "Product Na",
"kategorie": "Category",
"beschreibung": "Description",
"bild": "http:\/\/arsdecora.net\/wp-content\/uploads\/2015\/04\/B1696.jpg",
"preis": "0"
},
{
"pid": "1160",
"name": "Beispiel B",
"kategorie": null,
"beschreibung": null,
"bild": "http:\/\/arsdecora.net\/wp-content\/uploads\/2015\/04\/B1696.jpg",
"preis": "0"
}
]
}
这是您需要的 classes:
数据class:
public class Data {
private List<Product> products;
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
}
产品class:
public class Product {
private String pid;
private String name;
private String kategorie;
private String beschreigung;
private String bild;
private String preis;
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getKategorie() {
return kategorie;
}
public void setKategorie(String kategorie) {
this.kategorie = kategorie;
}
public String getBeschreigung() {
return beschreigung;
}
public void setBeschreigung(String beschreigung) {
this.beschreigung = beschreigung;
}
public String getBild() {
return bild;
}
public void setBild(String bild) {
this.bild = bild;
}
public String getPreis() {
return preis;
}
public void setPreis(String preis) {
this.preis = preis;
}
}
GsonTest class:
public class GsonTest {
public static void main(String[] args) {
Gson gson = new Gson();
Object obj;
try {
JsonParser parser = new JsonParser();
obj = parser.parse(new FileReader("C:\data.json"));
JsonObject jsonObject = (JsonObject) obj;
Data data = gson.fromJson(jsonObject, Data.class);
} catch (JsonIOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我想将 JSON对象(数组)转换为对象列表。 因为我是 java 的新手,所以遇到了一些问题。
JSON:
"products": [
{
"pid": "0",
"name": "Product Na",
"kategorie": "Category",
"beschreibung": "Description",
"bild": "http:\/\/arsdecora.net\/wp-content\/uploads\/2015\/04\/B1696.jpg",
"preis": "0"
},
{
"pid": "1160",
"name": "Beispiel B",
"kategorie": null,
"beschreibung": null,
"bild": "http:\/\/arsdecora.net\/wp-content\/uploads\/2015\/04\/B1696.jpg",
"preis": "0"
},
产品class:
public class Produkt {
public String id;
public String name;
public String categorie;
public String description;
public String image;
public double price;
}
我已经用 gson 尝试了几件事,但最终都没有奏效。 我不需要工作代码,只是关于如何通过标签反序列化 JSON 的提示。
希望你能帮助我。提前致谢!
尝试创建一个包含产品列表的 class。这是一个完整的例子:
在您的 json 数据周围添加方括号,如下所示:
{
"products": [
{
"pid": "0",
"name": "Product Na",
"kategorie": "Category",
"beschreibung": "Description",
"bild": "http:\/\/arsdecora.net\/wp-content\/uploads\/2015\/04\/B1696.jpg",
"preis": "0"
},
{
"pid": "1160",
"name": "Beispiel B",
"kategorie": null,
"beschreibung": null,
"bild": "http:\/\/arsdecora.net\/wp-content\/uploads\/2015\/04\/B1696.jpg",
"preis": "0"
}
]
}
这是您需要的 classes:
数据class:
public class Data {
private List<Product> products;
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
}
产品class:
public class Product {
private String pid;
private String name;
private String kategorie;
private String beschreigung;
private String bild;
private String preis;
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getKategorie() {
return kategorie;
}
public void setKategorie(String kategorie) {
this.kategorie = kategorie;
}
public String getBeschreigung() {
return beschreigung;
}
public void setBeschreigung(String beschreigung) {
this.beschreigung = beschreigung;
}
public String getBild() {
return bild;
}
public void setBild(String bild) {
this.bild = bild;
}
public String getPreis() {
return preis;
}
public void setPreis(String preis) {
this.preis = preis;
}
}
GsonTest class:
public class GsonTest {
public static void main(String[] args) {
Gson gson = new Gson();
Object obj;
try {
JsonParser parser = new JsonParser();
obj = parser.parse(new FileReader("C:\data.json"));
JsonObject jsonObject = (JsonObject) obj;
Data data = gson.fromJson(jsonObject, Data.class);
} catch (JsonIOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}