You'll have to play around with the schema to fix some properties that are missing like burgerSausce, etc. You get the idea though
Looks like you have malformed JSON. I believe what you're looking for is:
{"burger" :
{ "burgerTypes" :
[
{"Name": "Chicken Burger", "Price": 3.00},
{"Name": "Cheeseburger", "Price": 2.00},
{"Name": "beef burger", "Price": 2.00}
]
},
"burgerToppings" : [
{"Name": "Lettuce"},
{"Name": "Tomato"},
{"Name":"Cucumber"}
],
"burgerSauces" : [
{"Name": "Mayo"},
{"Name": "ketchup"},
{"Name": "Spicey mayo"}
]
}
I'd take that JSON, turn it into a schema, and run it through a converter to create some POJO's. Once you have the POJO's generated, use Jackson to do the work for you.
I went to http://www.jsonschema2pojo.org/ and generated the following:
-----------------------------------com.example.Burger.java-----------------------------------
package com.example;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"burgerTypes"
})
public class Burger {
@JsonProperty("burgerTypes")
public List<BurgerType> burgerTypes = null;
}
-----------------------------------com.example.BurgerSauce.java-----------------------------------
package com.example;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"Name"
})
public class BurgerSauce {
@JsonProperty("Name")
public String name;
}
-----------------------------------com.example.BurgerTopping.java-----------------------------------
package com.example;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"Name"
})
public class BurgerTopping {
@JsonProperty("Name")
public String name;
}
-----------------------------------com.example.BurgerType.java-----------------------------------
package com.example;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"Name",
"Price"
})
public class BurgerType {
@JsonProperty("Name")
public String name;
@JsonProperty("Price")
public Float price;
}
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"burger",
"burgerToppings",
"burgerSauces"
})
public class Example {
@JsonProperty("burger")
public Burger burger;
@JsonProperty("burgerToppings")
public List<BurgerTopping> burgerToppings = null;
@JsonProperty("burgerSauces")
public List<BurgerSauce> burgerSauces = null;
}
Now that you have your entities you can simply call:
Burger burger = objectMapper.readValue(json, Burger.class);