gpt4 book ai didi

数据表编辑器 json 的 Java 结构

转载 作者:行者123 更新时间:2023-12-02 03:34:39 24 4
gpt4 key购买 nike

我需要一个 Java 数据结构来存储数据表编辑器传递给我的一些 JSON 数据。接收到的数据格式是这样的:

{
"action":"edit",
"data": {
"1009558":{
"weekNumber":"2"
... (more properties)
}
}
}

以下是完整文档:https://editor.datatables.net/manual/server

编辑:文档显示作为表单参数发送的数据。我正在对数据进行字符串化并将其作为 JSON 发送。上面是一个例子。

“1009558”是行 ID。如果编辑器发送了多行,则会有多个数组条目(每个条目都有一个 ID)。

任何人都可以提供一些关于如何制作用于反序列化的 Java 数据结构(通过 Spring MVC)的建议吗?我可以很容易地映射“ Action ”,但我陷入了“数据”元素。

最佳答案

我宁愿建议您使用jackson .

这是您要求的示例:

package com.github.xsavikx.jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

public class JacksonTest {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
Map<String, DatabaseRow> data = new HashMap<>();
DatabaseRow row = new DatabaseRow(2, "someData");
data.put("1009558", row);
String action = "action";
DatabaseEntry dbEntry = new DatabaseEntry();
dbEntry.setAction(action);
dbEntry.setData(data);
System.out.println(objectMapper.writeValueAsString(dbEntry));
}
}

结果:

{"action":"action","data":{"1009558":{"weekNumber":2,"someData":"someData"}}}

型号:

package com.github.xsavikx.jackson;

import java.util.Map;

public class DatabaseEntry {
private String action;
private Map<String, DatabaseRow> data;

public DatabaseEntry() {

}

public DatabaseEntry(String action, Map<String, DatabaseRow> data) {
this.action = action;
this.data = data;
}

public Map<String, DatabaseRow> getData() {

return data;
}

public void setData(Map<String, DatabaseRow> data) {
this.data = data;
}

public String getAction() {
return action;
}

public void setAction(String action) {
this.action = action;
}
}

package com.github.xsavikx.jackson;

public class DatabaseRow {
private int weekNumber;
private String someData;
public DatabaseRow(){
}
public DatabaseRow(int weekNumber, String someData) {
this.weekNumber = weekNumber;
this.someData = someData;
}

public int getWeekNumber() {
return weekNumber;
}

public void setWeekNumber(int weekNumber) {
this.weekNumber = weekNumber;
}

public String getSomeData() {
return someData;
}

public void setSomeData(String someData) {
this.someData = someData;
}
}

更新:使用 map map 更通用的解决方案:

package com.github.xsavikx.jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class JacksonTest {
public static void main(String[] args) throws IOException {
serializeTest();
deserializeTest();
}
private static void deserializeTest() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
DatabaseEntry databaseEntry = objectMapper.readValue("{\"action\":\"action\",\"data\":{\"1009558\":{\"weekNumber\":2,\"someData\":\"someData\"}}}", DatabaseEntry.class);
System.out.println(databaseEntry);
}

private static void serializeTest() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
Map<String,Map<String,String>> data = new HashMap<>();
Map<String,String> values = new HashMap<>();
values.put("weekDay","2");
values.put("unpredictableValue","value");
data.put("1009558", values);
String action = "action";
DatabaseEntry dbEntry = new DatabaseEntry();
dbEntry.setAction(action);
dbEntry.setData(data);
System.out.println(objectMapper.writeValueAsString(dbEntry));
}
}

型号: 包 com.github.xsavikx.jackson;

import java.util.Map;

public class DatabaseEntry {
private String action;
private Map<String, Map<String,String>> data;

public DatabaseEntry() {

}

public String getAction() {
return action;
}

public void setAction(String action) {
this.action = action;
}

public Map<String, Map<String, String>> getData() {
return data;
}

public void setData(Map<String, Map<String, String>> data) {
this.data = data;
}
}

关于数据表编辑器 json 的 Java 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37594712/

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