解析ajax响应中的LocalDate和嵌套对象列表

Parsing LocalDate and list of nested objects in ajax response

本文关键字:嵌套 对象 列表 LocalDate ajax 响应 解析      更新时间:2023-09-26

有人能帮我吗。我正在设法解决两个问题。首先,如何在ajax响应中显示LocalDate。第二,对ajax响应中接收到的自定义对象列表进行迭代。

我在ajax响应中传递了自定义对象列表和localDate,但不知道如何在UI中显示它。日期字段正在显示[对象对象]。下面是我的ajax调用代码。

var table = $("#example").DataTable( {
        "bProcessing": true,
        "bServerSide": true,
        'sDom' : 'T<"clear">lrtip',
        "sort": "position",
        "sAjaxSource": "springPaginationDataTables.web",
        "aoColumns": [
            { "mData": "name" },
            { "mData": "position" },
            { "mData": "office" },
            { "mData": "phone" },
            { "mData": "start_date" },
            { "mData": "salary" },
            { "mData": "dob" },//LocalDate
            { "mData": "addresses" },//list of addresses
        ],
        columnDefs: [
                     {
                         targets: [ 6 ],
                         render: function ( data, type, row ) {
                             console.log(type);
                             return data;//process LocalDate here
                         }
                     },
                     {targets: [ 7 ],
                         render: function ( data, type, row ) {
                             console.log(type);
                             return data;//process addresses list here
                         }
                     }
                    ]
    } );

从MVC控制器,我将人员列表传递到JSON对象中,如下所示

PersonJsonObject personJsonObject = new PersonJsonObject();
        //Set Total display record
        personJsonObject.setiTotalDisplayRecords(500);
        //Set Total record
        personJsonObject.setiTotalRecords(500);
        personJsonObject.setAaData(personsList);
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json2 = gson.toJson(personJsonObject);
        return json2;

PersonJsonObject是Jquery Datatable类型的对象,它保存要在UI上显示的记录跟踪和数据列表。Person对象具有地址对象的列表。如何在得到list-say data.getaddresss后对列表进行迭代并将其存储在变量中。我知道如何使用jsp标签迭代列表,但不知道如何在Jquery 中进行同样的操作

已解决。对于LocalDate问题,需要注册自定义模块。有关详细信息,请查看此链接。Ajax响应未解析LocalDate

对于在列表上迭代,使用简单的jquery foreach循环

$j.each(data, function (index, value) {
        var temp = value.field1+" "+value.field1+" ";//field1 and field2 are fields of custom object
        console.log(temp);
   return temp;
  });