简体   繁体   中英

Parsing Data From JSON file to Java Object using GSON

I have this extremely long JSON file that has a structure like this

{
 "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:

This is the container holding the count item as well as the list of tags called 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. The link to downloading this extremely long JSON file is here Really Long JSON File . If you save the page as a.json it is around 121MB so just keep that noted.

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 .

If you want to keep your object structure, you can check here how to do it with a Custom Deserializer or Annotations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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