gpt4 book ai didi

java - 使用 Java 和 Jackson 将数组写入 JSON

转载 作者:行者123 更新时间:2023-12-01 21:57:19 26 4
gpt4 key购买 nike

我需要通过读取 csv 来创建一个如下所示的 JSON 结构。

[
{
"firstName": "John",
"lastName": "Smith",
"udas": [
{
"name": "UDF1",
"dataType": "number",
"value": 0
}
]
}
]

我得到的结构是

[{
"firstName": "Michael",
"lastName": "Dodd"
}, {
"udas": [{
"name": "UDF1",
"dataType": "number",
"entityCode": 0
}]
}]

从文件中读取没有问题,但格式化 JSON 是一个问题。如何去掉“udas”数组周围多余的大括号?

这是我正在使用的代码。

public class MyTestClass {
public static void main(String[] args) {
String row;
String[] data = {};
ObjectMapper objectMapper = new ObjectMapper();

try {
BufferedReader csvReader = new BufferedReader(new FileReader("res/mydata.csv"));
row = csvReader.readLine();
data = row.split(",");

csvReader.close();
} catch (IOException e) {
e.printStackTrace();
}

ArrayNode arrayNode = objectMapper.createArrayNode();

ObjectNode personObjectNode = objectMapper.createObjectNode();
personObjectNode.put("firstName", data[0]);
personObjectNode.put("lastName", data[1]);
arrayNode.add(personObjectNode);

ArrayNode udaArrayNode = objectMapper.createArrayNode();

ObjectNode approvalAuthorityObjectNode = objectMapper.createObjectNode();
approvalAuthorityObjectNode.put("name", "UDF1");
approvalAuthorityObjectNode.put("dataType", "number");
approvalAuthorityObjectNode.put("entityCode", Integer.parseInt(data[15]));
udaArrayNode.add(approvalAuthorityObjectNode);

ObjectNode udaObjectNode = objectMapper.createObjectNode();
udaObjectNode.putPOJO("udas", udaArrayNode);
arrayNode.add(udaObjectNode);

System.out.println(arrayNode.toString());
}
}

最佳答案

以下更改解决了该问题。谢谢,@SotiriosDelimanolis。

public class MyTestClass{
public static void main(String[] args) {
String row;
String[] data = {};
ObjectMapper objectMapper = new ObjectMapper();

try {
BufferedReader csvReader = new BufferedReader(new FileReader("res/mydata.csv"));
row = csvReader.readLine();
data = row.split(",");

csvReader.close();
} catch (IOException e) {
e.printStackTrace();
}

ArrayNode arrayNode = objectMapper.createArrayNode();

ObjectNode personObjectNode = objectMapper.createObjectNode();
personObjectNode.put("firstName", data[0]);
personObjectNode.put("lastName", data[1]);
arrayNode.add(personObjectNode);

ArrayNode udaArrayNode = objectMapper.createArrayNode();
ObjectNode approvalAuthorityObjectNode = objectMapper.createObjectNode();
approvalAuthorityObjectNode.put("name", "UDF1");
approvalAuthorityObjectNode.put("dataType", "number");
approvalAuthorityObjectNode.put("entityCode", Integer.parseInt(data[15]));

udaArrayNode.add(approvalAuthorityObjectNode);

personObjectNode.putPOJO("udas", udaArrayNode);

System.out.println(arrayNode.toString());
}
}

现在我得到了 JSON

[{
"firstName": "Michael",
"lastName": "Dodd",
"udas": [{
"name": "UDF1",
"dataType": "number",
"entityCode": 0
}]
}]

关于java - 使用 Java 和 Jackson 将数组写入 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58734479/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com