[英]converting JSONObject to Object list
I want to convert a JSONObject (array) to a list of Objects.我想将 JSONObject(数组)转换为对象列表。 As I am very new with java i am having quite some problems.因为我对java很陌生,所以我遇到了很多问题。
JSON: 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"
},
Product class:产品类别:
public class Produkt {
public String id;
public String name;
public String categorie;
public String description;
public String image;
public double price;
}
I have tried several things with gson, but ultimately nothing worked.我用 gson 尝试了几件事,但最终没有任何效果。 I don't need a working code, just a hint on how to deserialize the JSON by the tags.我不需要工作代码,只是提示如何通过标签反序列化 JSON。
I hope you can help me.我希望你能帮助我。 Thanks in advance!提前致谢!
Considering that your request or string data is in JSONObject jsonArray
.考虑到您的请求或字符串数据位于JSONObject jsonArray
中。 Below code can help you get the response in List using TypeToken .下面的代码可以帮助您使用TypeToken在 List 中获得响应。
JSONArray jsonArray = jsonResponse.getJSONArray("products");
String newList = jsonArray.toString();
Gson gson = new Gson();
Type typeOfProduktList = new TypeToken<ArrayList<Produkt>>() {}.getType();
List<Produkt> finalList = gson.fromJson(newList, typeOfProduktList);
Now, you can return the finalList
in the end or process it as per your wish.现在,您可以最终返回finalList
或根据您的意愿对其进行处理。
Try creating a class that has a list of products.尝试创建一个包含产品列表的类。 Here is a complete example:这是一个完整的例子:
Add brackets around your json data like this:在您的 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"
}
]
}
Here are the classes you need:以下是您需要的课程:
Data class:数据类:
public class Data {
private List<Product> products;
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
}
Product 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: GsonTest 类:
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.