Reputation: 15
I'm trying to map a JSON file to a set of classes I've created in Java. Little background on what I'm trying to accomplish. I'm trying to may a "Pot -> Seed -> Harvest Basket -> Harvest Item" where: Pot: Brass, Clay, Wooden Seed: Grain, Fruit Harvest Basket: (a list of items that the pot/seed will return) Harvest Content: The item and quantity range
Here's the JSON I've setup (be gentle, I'm 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}
]
}
]
}
]
}
]
}
I have the following classes setup. Note, I've manually added values to the classes first, and am now trying to get the JSON to assign the values, so in theory, the class setup should be good.
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;
}
}
So, I'm using GSON, and it appears to be reading in the file, but when I output on anything, I'm getting nullPointer exceptions. I'm not sure if the import isn't working or what... Here's the code I'm messing with:
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());
The console response I get is:
1717159510
Response@6659c656
Exception in thread "main" java.lang.NullPointerException
at PlantsMain.JSONAssign(PlantsMain.java:56)
at PlantsMain.main(PlantsMain.java:36)
I tried other things, like passing to the Get function, but I figured if I can't get the class type returned, I've got bigger problems.
Upvotes: 1
Views: 522
Reputation: 546
Define these classes:
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;
}
Then use:
Gson gson = new Gson();
Pots pots = gson.fromJson("Your JSON content", Pots.class);
Hope it helps.
Upvotes: 1