【问题标题】:How to create a nested array inside an array in a json with Java?如何使用Java在json中的数组内创建嵌套数组?
【发布时间】:2023-03-29 15:45:02
【问题描述】:

我正在尝试弄清楚如何使用 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 中创建一个空数组列表,或者这将如何适合我的代码示例。如果有人可以建议做什么,我希望您适当地采用/修改我的代码,因为这对我来说更容易/更有意义。感谢任何帮助,谢谢! :)

【问题讨论】:

  • 需要的结构是List&lt;List&lt;String&gt;&gt; color
  • 正如 Nikolas 所说,您必须将 color 属性更改为 List&lt;List&lt;String&gt;&gt;

标签: java arrays json jackson


【解决方案1】:

根据您的 json 结构将 public List&lt;String&gt; color; 更改为 public List&lt;List&lt;String&gt;&gt; color;

填充它的逻辑:

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

fruitInventory.setValues( Arrays.asList(color));

【讨论】:

  • 似乎是正确的建议。关于此更改后我得到的异常的任何想法:不兼容的类型。必需:列表 > ||找到:列表
  • 您是否在构造函数中将参数类型从 list 更改为 listOfList?
  • 将构造函数签名从 public FruitInventory(List&lt;String&gt; fruit, List&lt;String&gt; color, String edible) 更改为 public FruitInventory(List&lt;String&gt; fruit, List&lt;List&lt;String&gt;&gt; color, String edible)
猜你喜欢
  • 2021-08-24
  • 1970-01-01
  • 2019-02-27
  • 1970-01-01
  • 1970-01-01
  • 2018-11-27
  • 2020-10-29
  • 2018-10-28
相关资源
最近更新 更多