简体   繁体   English

数据表编辑器 json 的 Java 结构

[英]Java structure for Datatables editor json

I need a Java data structure for some JSON data passed to me by the Datatables Editor.对于数据表编辑器传递给我的一些 JSON 数据,我需要一个 Java 数据结构。 The format of the data received is this:接收到的数据格式是这样的:

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

Here's the full documentation: https://editor.datatables.net/manual/server这是完整的文档: https : //editor.datatables.net/manual/server

Edit: The documentation shows the data sent as form params.编辑:文档显示作为表单参数发送的数据。 I am stringifying the data and sending it as JSON.我正在对数据进行字符串化并将其作为 JSON 发送。 An example is above.上面有一个例子。

"1009558" is the row ID. “1009558”是行 ID。 If there are multiple rows sent by the editor, there would be multiple array entries (each with an ID).如果编辑器发送了多行,就会有多个数组条目(每个条目都有一个 ID)。

Can anyone offer some advice on how to make a Java data structure for deserialization (by Spring MVC)?任何人都可以就如何制作用于反序列化的 Java 数据结构(通过 Spring MVC)提供一些建议吗? I can map "action" easy enough, but I'm getting stuck on the "data" element.我可以很容易地映射“动作”,但我被困在“数据”元素上。

I'm a huge fan of Joe Littlejohn's JSON tool .我是 Joe Littlejohn 的JSON 工具的超级粉丝。 Provide it with a sample JSON file and it can generate POJOs for you.为它提供一个示例 JSON 文件,它可以为您生成 POJO。

Here's a sample of what it generated, based on a snipped of JSON from the site you posted.这是它生成的示例,基于您发布的站点中的 JSON 片段。

JSON: JSON:

{
"data": [
    {
        "DT_RowId":   "row_29",
        "first_name": "Fiona",
        "last_name":  "Green",
        "position":   "Chief Operating Officer (COO)",
        "office":     "San Francisco",
        "extn":       "2947",
        "salary":     "850000",
        "start_date": "2010-03-11"
    }
]

} }

JAVA:爪哇:

@Generated("org.jsonschema2pojo")
public class Datum {

    public String dTRowId;
    public String firstName;
    public String lastName;
    public String position;
    public String office;
    public String extn;
    public String salary;
    public String startDate;
}

@Generated("org.jsonschema2pojo")
public class Example {
    public List<Datum> data = new ArrayList<Datum>();
}

Update:更新:

It looks like this is what the form submit actually sends:看起来是表单提交实际发送的内容:

action:edit
data[row_1][first_name]:Tiger23
data[row_1][last_name]:Nixon
data[row_1][position]:System Architect
data[row_1][office]:Edinburgh
data[row_1][extn]:5421
data[row_1][start_date]:2011-04-25
data[row_1][salary]:320800

I don't think this is Json, and I dunno if I would try to treat it as such.我不认为这是 Json,我不知道我是否会尝试这样对待它。 If you need to submit form data with Java, you might be better of using the Apache HttpComponents .如果您需要使用 Java 提交表单数据,最好使用Apache HttpComponents You can reuse the Java "data" object above as a domain object, and then populate the POST content with Strings of the format:您可以将上面的 Java“数据”对象重用为域对象,然后使用以下格式的字符串填充 POST 内容:

data[ \DT_RowId\ ][\PropertyName\]: \PropertyValue\

I'd rather suggest you to use jackson .我宁愿建议你使用jackson

Here's an example, that you're asking for:这是您要求的示例:

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

And the result:结果:

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

Models:楷模:

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

Update: more generic solution with Map of maps:更新:更通用的地图地图解决方案:

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

Model: package com.github.xsavikx.jackson;模型:包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;
    }
}

With Spring Boot the json conversion between server and client is automatic ( https://stackoverflow.com/a/44842806/3793165 ).使用 Spring Boot,服务器和客户端之间的 json 转换是自动的( https://stackoverflow.com/a/44842806/3793165 )。

This way is working for me:这种方式对我有用:

Controller控制器

@PostMapping(value="/nuevoVideo")
    @ResponseBody
    public RespuestaCreateVideo crearNuevoVideo(@RequestBody PeticionVideos datos) {
       RespuestaCreateVideo respuesta = new RespuestaCreateVideo();
       respuesta.setData("datos");
       //respuesta.setError("error"); // implement the logic for error and return the message to show to the user.
      return respuesta;
    }

where PeticionVideos (RequestVideos) is the create structure Datatables editor sends (with setters, getters...):其中 PeticionVideos (RequestVideos) 是数据表编辑器发送的创建结构(带有 setter、getter...):

public class PeticionVideos {
    private Map<String, Video> data;
    private String action;
}

The response from the server to the client datatable is waiting for has a particular format (check at https://editor.datatables.net/manual/server ).从服务器到客户端数据表正在等待的响应具有特定格式(请查看https://editor.datatables.net/manual/server )。

I often use this:我经常使用这个:

public class RespuestaCreateVideo { //ResponseCreateVideo
    private String data;
    private String error;
}

After two days trying this is working perfect now!经过两天的尝试,现在工作完美了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM