簡體   English   中英

從 JSON 文件中解析數據到 Java Object 使用 ZB0AA3DCF498BB14C701ADB49E3

[英]Parsing Data From JSON file to Java Object using GSON

我有這個非常長的 JSON 文件,其結構如下

{
 "count":123456,
 "tags":[
         {
          "sameAs":["https://www.wikidata.org/wiki/Q11254"],
          "url":"https://world.openfoodfacts.org/ingredient/salt",
          "products":214841,
          "name":"Salt",
          "id":"en:salt"
         },
         {
          "url":"https://world.openfoodfacts.org/ingredient/sugar",
          "sameAs":["https://www.wikidata.org/wiki/Q11002"],
          "name":"Sugar",
          "id":"en:sugar",
          "products":184348
         },
          ...
        ]

內部標簽對象的順序不會保持不變,但我認為這不會造成問題。 目前這是我用來解析這個 JSON Object 的代碼:

這是包含計數項目的容器以及名為 IngredientItem 的標簽列表。

public class Ingredients {
    private int count;
    private List<IngredientItem>  items;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public List<IngredientItem> getItems() {
        return items;
    }

    public void setItems(List<IngredientItem> items) {
        this.items = items;
    }
}

這是每個標簽的代碼:

public class IngredientItem {
    private List<String> sameAs;
    private String id;
    private String name;
    private String url;
    private int productNumber;

    public IngredientItem(List<String> sameAs, String id, String name, String url, int productNumber) {
        this.sameAs = sameAs;
        this.id = id;
        this.name = name;
        this.url = url;
        this.productNumber = productNumber;
    }

    public List<String> getSameAs() {
        return sameAs;
    }

    public void setSameAs(List<String> sameAs) {
        this.sameAs = sameAs;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getProductNumber() {
        return productNumber;
    }

    public void setProductNumber(int productNumber) {
        this.productNumber = productNumber;
    }

    @Override
    public String toString() {
        return "product number: " + getProductNumber() +
                "\n" + "name: " + getName() +
                "\n" + "id: " + getId() +
                "\n" + "same as: " + getSameAs() +
                "\n" + "url: " + getUrl();
    }
}

這是我實際解析它的主要代碼。

        Gson gson = new Gson();

        FileReader fr = new FileReader("path\\to\\file\\ingredients.json");
        Ingredients ingredients = gson.fromJson(fr,Ingredients.class);

        if(ingredients.getItems() ==null){
            System.out.println("NULL");
        }else{
            for (IngredientItem item: ingredients.getItems()) {
                System.out.println(item.toString());
            }
        }

由於某種原因,它永遠不會填滿所有標簽中的項目。 I have already extensively looked at this Parsing a complex Json Object using GSON in Java question and I cannot seem to find the error. 下載這個極長的 JSON 文件的鏈接在這里真的很長 JSON 文件 如果您將頁面保存為 a.json,它大約為 121MB,因此請注意。

先感謝您。 如果需要任何其他信息,請讓我

要使其自動運行,您需要將Ingredients.items更改為Ingredients.tags

如果你想保留你的 object 結構,你可以在這里查看如何使用自定義反序列化器或注釋來做到這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM