将JSONObject转换为Object列表

时间:2015-07-30 21:20:12

标签: java arrays json serialization jsonobject

我想将JSONObject(数组)转换为对象列表。 因为我对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"
},

产品类别:

public class Produkt {

public String id;
public String name;
public String categorie;
public String description;
public String image;
public double price;
}

我用gson尝试了几件事,但最终没有任何效果。 我不需要工作代码,只是提示如何通过标记反序列化JSON。

我希望你能帮助我。提前谢谢!

1 个答案:

答案 0 :(得分:0)

尝试创建一个包含产品列表的类。这是一个完整的例子:

在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"
        }
    ]
}

以下是您需要的课程:

数据类:

public class Data {
    private List<Product> products;

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }
}

产品类别:

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课程:

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();
        }
    }
}