简体   繁体   English

从 JSON 文件中解析数据到 Java Object 使用 ZB0AA3DCF498BB14C701ADB49E3

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

I have this extremely long JSON file that has a structure like this我有这个非常长的 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
         },
          ...
        ]

The order of the inner tag objects do not remain the same but i dont think that would pose a problem.内部标签对象的顺序不会保持不变,但我认为这不会造成问题。 Currently this is the code that im using to parse this JSON Object:目前这是我用来解析这个 JSON Object 的代码:

This is the container holding the count item as well as the list of tags called IngredientItem.这是包含计数项目的容器以及名为 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;
    }
}

This is the code for each tag:这是每个标签的代码:

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

and this is my main code to actually parse it.这是我实际解析它的主要代码。

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

for some reason it wont ever fill up the items from all the tags.由于某种原因,它永远不会填满所有标签中的项目。 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. 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. The link to downloading this extremely long JSON file is here Really Long JSON File .下载这个极长的 JSON 文件的链接在这里真的很长 JSON 文件 If you save the page as a.json it is around 121MB so just keep that noted.如果您将页面保存为 a.json,它大约为 121MB,因此请注意。

Thank you in advance.先感谢您。 If any other information is required please let m如果需要任何其他信息,请让我

For it to be automatic, you need to change Ingredients.items to Ingredients.tags .要使其自动运行,您需要将Ingredients.items更改为Ingredients.tags

If you want to keep your object structure, you can check here how to do it with a Custom Deserializer or Annotations.如果你想保留你的 object 结构,你可以在这里查看如何使用自定义反序列化器或注释来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM