0

I am trying to figure out how to create a nested array inside an array with Java. See below:

{
    "fruit": [
        "apple"
    ],
    "color": [
        [                         <------ trying this!
            "red",
            "green"
        ]                         <------ trying this?
    ],
    "edible": true
}

But I only managed to do this:

{
    "fruit": [
        "apple"
    ],
    "color": [
        "red",
        "green"
    ],
    "edible": true
}

And so far... this is the code I have:

Class with logic to create the json object

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;
    }

}

Method to print 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);
}

I am not sure if I should create an empty array list inside color or how this will fit into my code example. If someone could suggest what to do and I would prefer if you take/modify my code as appropriate as it would be easier/make more sense to me. Appreciate any help, thanks! :)

2
  • 3
    The required structure is List<List<String>> color. Commented Dec 21, 2018 at 11:34
  • 1
    As Nikolas has said, you will have to change the color property to List<List<String>> Commented Dec 21, 2018 at 11:54

1 Answer 1

3

Change public List<String> color; to public List<List<String>> color; as per your json structure.

Logic to populate it:

List<String> color = new ArrayList<>();
color.add("red");
color.add("green");

fruitInventory.setValues( Arrays.asList(color));
Sign up to request clarification or add additional context in comments.

3 Comments

Seems right suggestion. Any idea about the exceptions I am getting after this change: Incompatible types. Required: List <java.util.List<java.lang.String>> || Found: List <java.lang.String>
Did you change parameter type from list to listOfList in that constructor?
Change your constructor signature from public FruitInventory(List<String> fruit, List<String> color, String edible) to public FruitInventory(List<String> fruit, List<List<String>> color, String edible)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.