从JSON文件到Java类的GSON映射

从JSON文件到Java类的GSON映射,java,json,gson,Java,Json,Gson,我试图将一个JSON文件映射到我用Java创建的一组类。关于我想要完成的事情的一些背景知识。我试图制作一个“花盆->种子->收获篮->收获物品”,其中: 锅:黄铜、粘土、木制 种子:谷物、水果 收获篮:(花盆/种子将返回的项目列表) 收获内容:项目和数量范围 以下是我设置的JSON(请温柔一点,我是n00b): 我有以下的课程设置。注意,我已经首先手动向类添加了值,现在正试图让JSON来分配这些值,因此理论上,类设置应该是好的 public class Pot { String potType;

我试图将一个JSON文件映射到我用Java创建的一组类。关于我想要完成的事情的一些背景知识。我试图制作一个“花盆->种子->收获篮->收获物品”,其中: 锅:黄铜、粘土、木制 种子:谷物、水果 收获篮:(花盆/种子将返回的项目列表) 收获内容:项目和数量范围

以下是我设置的JSON(请温柔一点,我是n00b):

我有以下的课程设置。注意,我已经首先手动向类添加了值,现在正试图让JSON来分配这些值,因此理论上,类设置应该是好的

public class Pot {
String potType;
List <Seed> seedType;

public Pot(String potType, List<Seed> seedItems) {
    this.potType = potType;
    this.seedType = seedItems;
}

public String getPotType() {
    return this.potType;
}

public void setPotType(String potType) {
    this.potType = potType;
}
}

public class Seed {
String seedType;
List <HarvestBasket> harvestBasket;

public Seed(String seedType, List <HarvestBasket> harvestBasket) {
    this.seedType = seedType;
    this.harvestBasket = harvestBasket;
}
}

public class HarvestBasket {
int harvestBasketID;
String harvestBasketName;
int chance;
List <HarvestContent> harvestContent;

public HarvestBasket (int harvestBasketID, String harvestBasketName, int chance, List <HarvestContent> harvestContent) {
    this.harvestBasketID = harvestBasketID;
    this.harvestBasketName = harvestBasketName;
    this.chance = chance;
    this.harvestContent = harvestContent;
}

}

public class HarvestContent {
String harvestItem;
int min, max;

public HarvestContent(String harvestItem, int min, int max) {
    this.harvestItem = harvestItem;
    this.min = min;
    this.max = max;
}
}

public class HarvestContent {
String harvestItem;
int min, max;

public HarvestContent(String harvestItem, int min, int max) {
    this.harvestItem = harvestItem;
    this.min = min;
    this.max = max;
}
}
我尝试了其他方法,比如传递给Get函数,但我认为如果无法返回类类型,我会遇到更大的问题。

定义这些类:

class Pots {
    List<Pot> Pot;
  }

  class Pot {
    protected String potType;
    protected List<Seed> Seed;
  }

  class HarvestBasket {
    protected String harvestBasketID;
    protected String harvestBasketName;
    protected int chance;
    protected List<HarvestContent> harvestContent;
  }

  class HarvestContent {
    protected String harvestItem;
    protected int min;
    protected int max;
  }

  class Seed {
    protected String seedType;
    protected List<HarvestBasket> HarvestBasket;
  }

希望有帮助。

谢谢。我明天早上去试试。你能解释一下你的解决方案为什么有效吗?我正在努力学习。。。是不是把类名映射到我搞砸的JSON上了?@PatK:请参考这个,关键字是“gson嵌套JSON解析”。谢谢。我知道我做错了什么。。。没有确保类名和变量与JSON匹配。
class Response {
Map<String, Pot> myPot;

public Map<String, Pot> getPot() {
    return myPot;
}
}

    public static void JSONAssign() {

    Gson gson = new Gson();
    BufferedReader br = null;


    try {
        br = new BufferedReader(new FileReader("/Users/patkhumprakob/Documents/workspace/Plants/src/harvestLoot.json"));

        Response response;
        try {
            response = gson.fromJson(br, Response.class);
            System.out.println(response.hashCode());
            System.out.println(response.toString());

            System.out.println(response.getPot().getClass());
1717159510
Response@6659c656
Exception in thread "main" java.lang.NullPointerException
at PlantsMain.JSONAssign(PlantsMain.java:56)
at PlantsMain.main(PlantsMain.java:36)
class Pots {
    List<Pot> Pot;
  }

  class Pot {
    protected String potType;
    protected List<Seed> Seed;
  }

  class HarvestBasket {
    protected String harvestBasketID;
    protected String harvestBasketName;
    protected int chance;
    protected List<HarvestContent> harvestContent;
  }

  class HarvestContent {
    protected String harvestItem;
    protected int min;
    protected int max;
  }

  class Seed {
    protected String seedType;
    protected List<HarvestBasket> HarvestBasket;
  }
Gson gson = new Gson();
Pots pots = gson.fromJson("Your JSON content", Pots.class);