gpt4 book ai didi

java - 如何在spring mvc Controller 中读取具有多个数组的json?

转载 作者:行者123 更新时间:2023-11-30 07:25:24 25 4
gpt4 key购买 nike

我正在将 json 从 ajax 发送到包含多个数组的 Controller 。

Json 数据:

{
"node": [
{
"id": "dev_1",
"blockId": "11"
},
{
"id": "dev_2",
"blockId": "15"
}
],
"connect": [
{
"id": "con_5",
"typeId": "2"
}
],
"name": "test"
}

Controller :

@RequestMapping(value = "/saveBoard", method = RequestMethod.POST)
public JsonResponse saveBoard(@RequestBody String jsonData) throws IOException {
JsonResponse response = new JsonResponse();
ObjectMapper mapper = new ObjectMapper();
final JsonNode jsonNode = mapper.readTree(jsonData).get("node");

if(jsonNode.isArray()) {
for (final JsonNode nodes : jsonNode) {
System.out.println("jsonNode : "+ nodes);
}
}
return response;
}

我尝试过使用对象映射器但没有成功。在这里,我想读取不同类的不同数组,例如具有某些指定字段的节点类的节点,用于连接类的连接和用于其他用途的字符串。

更新 Controller :

@RequestMapping(value = "/saveBoard", method = RequestMethod.POST)
public JsonResponse saveMissionBoard(@RequestBody MissionJsonPojo chartJson) {
boolean status = false;
String messsage = "";
JsonResponse response = new JsonResponse();
System.out.println("data : " + flowChartJson.getNodes());
return response;
}

Ajax :

 $.ajax({
url: '<c:url value="/board/saveBoard"/>',
type: 'POST',
data: JSON.stringify(chartJson),
dataType: "json",
contentType: "application/json",
success: function(response) {
console.log("In success");
},
error: function (a, b, c) { }
});

JSON:

{
"nodes": [
{
"missionDeviceId": "device_1",
"device": "1",
"deviceXCoordinate": 79,
"deviceYCoordinate": 73,
"blockId": "1##1"
},
{
"missionDeviceId": "device_2",
"device": "3",
"deviceXCoordinate": 340,
"deviceYCoordinate": 146,
"blockId": "2##5"
}
],
"connections": [
{
"missionConnectionId": "con_5",
"sourceId": "device_1",
"targetId": "device_2",
"device": "2"
}
],
"name": "test"
}

节点 Pojo:

public class Nodes{
private String missionDeviceId;
private Integer device;
private String deviceXCoordinate;
private String deviceYCoordinate;
private String blockId; //getters setters
}

连接Pojo:

public class Connections{    
private String missionConnectionId;
private String sourceId;
private String targetId;
private Integer device; //getters and setters
}

MissionJsonPojo:

public class MissionJsonPojo{
private List<Nodes> nodes;
private List<Connections> connections;
private String name; //getters and setters
}

最佳答案

按照@dambros的建议,创建一个像这样的POJO结构:

public class Node {
private String id;
private String blockId;
//setter-getters.
}

public class Connect {
private String id;
private String typeId;
//setter-getters.
}

import java.util.List;

public class Payload {
private List<Node> nodes;
private List<Connect> connects;
private String name;
//setter-getters
}

并将方法签名更改为:

public JsonResponse saveBoard(@RequestBody Payload payload) throws IOException {

}

这应该可以解决您的问题。

关于java - 如何在spring mvc Controller 中读取具有多个数组的json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36884227/

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