【问题标题】:converting JSONObject to Object list将 JSONObject 转换为对象列表
【发布时间】:2015-07-30 21:20:12
【问题描述】:

我想将 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。

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

【问题讨论】:

  • 您显示的 JSON 不完整。 products 是一个 JSON 数组,将映射到 List<Produkt>。但是包含products 的JSON 不会。
  • 尝试将变量名与 json 键匹配

标签: java arrays json serialization jsonobject


【解决方案1】:

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

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

【讨论】:

  • 非常感谢您的回答!我会尝试实现它并告诉你它是如何进行的!
  • 它就像一个魅力!非常感谢 codemonkey!
猜你喜欢
  • 2017-12-27
  • 2016-05-14
  • 1970-01-01
  • 2014-08-10
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多