我正在尝试将 JSON 文件映射到我在 Java 中创建的一组类。关于我想要完成的任务的背景很少。我正在尝试“锅 -> 种子 -> 收获篮 -> 收获元素”,其中: 锅:黄铜、粘土、木制 种子: Cereal 、水果 收获篮:(盆栽/种子将返回的元素列表) 收获内容:元素及数量范围
这是我设置的 JSON(温柔点,我是 n00b):
{
"Pot": [
{"potType": "Clay",
"Seed": [
{"seedType": "Grain",
"HarvestBasket" : [
{"harvestBasketID": 1, "harvestBasketName": "temp", "chance": 25,
"harvestContent": [
{"harvestItem": "Wheat", "min": 3, "max": 6},
{"harvestItem": "Corn", "min": 1, "max": 3}
]
},
{"harvestBasketID": 2, "harvestBasketName": "temp", "chance": 50,
"harvestContent": [
{"harvestItem": "Rice", "min": 10, "max": 12}
]
},
{"harvestBasketID": 3, "harvestBasketName": "temp", "chance": 25,
"harvestContent": [
{"harvestItem": "Corn", "min": 1, "max": 3},
{"harvestItem": "Spelt", "min": 5, "max": 6}
]
}
]
},
{"seedType": "Fruit",
"HarvestBasket" : [
{"harvestBasketID": 1, "harvestBasketName": "temp", "chance": 25,
"harvestContent": [
{"harvestItem": "Apple", "min": 3, "max": 5},
{"harvestItem": "Orange", "min": 1, "max": 3}
]
},
{"harvestBasketID": 2, "harvestBasketName": "temp", "chance": 50,
"harvestContent": [
{"harvestItem": "Tangerine", "min": 10, "max": 12}
]
},
{"harvestBasketID": 3, "harvestBasketName": "temp", "chance": 25,
"harvestContent": [
{"harvestItem": "Kiwi", "min": 1, "max": 3},
{"harvestItem": "Apple", "min": 5, "max": 6}
]
}
]
}
]
},
{"potType": "Brass",
"Seed": [
{"seedType": "Grain",
"HarvestBasket" : [
{"harvestBasketID": 1, "harvestBasketName": "temp", "chance": 25,
"harvestContent": [
{"harvestItem": "Wheat", "min": 20, "max": 20},
{"harvestItem": "Corn", "min": 20, "max": 20}
]
},
{"harvestBasketID": 2, "harvestBasketName": "temp", "chance": 50,
"harvestContent": [
{"harvestItem": "Rice", "min": 20, "max": 20}
]
},
{"harvestBasketID": 3, "harvestBasketName": "temp", "chance": 25,
"harvestContent": [
{"harvestItem": "Corn", "min": 1, "max": 3},
{"harvestItem": "Spelt", "min": 20, "max": 20}
]
}
]
},
{"seedType": "Fruit",
"HarvestBasket" : [
{"harvestBasketID": 1, "harvestBasketName": "temp", "chance": 25,
"harvestContent": [
{"harvestItem": "Apple", "min": 25, "max": 25},
{"harvestItem": "Orange", "min": 25, "max": 25}
]
},
{"harvestBasketID": 2, "harvestBasketName": "temp", "chance": 50,
"harvestContent": [
{"harvestItem": "Tangerine", "min": 25, "max": 25}
]
},
{"harvestBasketID": 3, "harvestBasketName": "temp", "chance": 25,
"harvestContent": [
{"harvestItem": "Kiwi", "min": 25, "max": 25},
{"harvestItem": "Apple", "min": 25, "max": 25}
]
}
]
}
]
}
]
}
我有以下类(class)设置。请注意,我首先手动向类添加值,现在尝试获取 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;
}
}
所以,我正在使用 GSON,它似乎正在读取文件,但是当我输出任何内容时,我收到 nullPointer 异常。我不确定导入是否不起作用或者是什么...这是我正在弄乱的代码:
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)
我尝试了其他方法,例如传递给 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;
}
然后使用:
Gson gson = new Gson();
Pots pots = gson.fromJson("Your JSON content", Pots.class);
希望有帮助。
关于java - GSON 从 JSON 文件到 Java 类的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42406789/