Я пытаюсь выяснить, как создать вложенный массив внутри массива с помощью Java.См. Ниже:
{
"fruit": [
"apple"
],
"color": [
[ <------ trying this!
"red",
"green"
] <------ trying this?
],
"edible": true
}
Но мне удалось сделать это только:
{
"fruit": [
"apple"
],
"color": [
"red",
"green"
],
"edible": true
}
И пока ... это код, который у меня есть:
Класс с логикой для создания объекта json
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
public class FruitInventory {
public List<String> fruit;
public List<String> color;
private String edible;
public FruitInventory() {
}
public FruitInventory(List<String> fruit, List<String> color, String edible) {
this.fruit = fruit;
this.color = color;
this.edible = edible;
}
@JsonProperty("fruit")
public List<String> getfruit() {
return fruit;
}
@JsonProperty("fruit")
public void setfruit(List<String> fruit) {
this.fruit = fruit;
}
@JsonProperty("color")
public List<String> getcolor() {
return color;
}
@JsonProperty("color")
public void setcolor(List<String> color) {
this.color = color;
}
@JsonProperty("edible")
public String getedible() {
return edible;
}
@JsonProperty("edible")
public void setedible(String edible) {
this.edible = edible;
}
}
Метод печати json
public static void main(String[] args) throws JsonProcessingException {
FruitInventory fruitInventory = new FruitInventory();
String json;
ObjectMapper mapper = new ObjectMapper();
List<String> fruit = new ArrayList<>();
fruit.add("apple")
List<String> color = new ArrayList<>();
color.add("red");
color.add("green");
fruitInventory.setColumnNames(fruit);
fruitInventory.setValues(color);
json = mapper.writeValueAsString(fruitInventory);
System.out.println(json);
}
Я не уверен, стоит ли создавать пустойсписок массивов внутри color
или как это будет соответствовать моему примеру кода.Если бы кто-то мог предложить, что делать, и я бы предпочел, чтобы вы взяли / изменили мой код так, как это было бы проще / было бы более логично для меня.Ценю любую помощь, спасибо!:)