JSON Parse不能使用json字符串

时间:2017-05-09 08:25:54

标签: jquery .net json datatable

我有代码

var JsonData = JsonConvert.SerializeObject(Data);

使用转储数据转换为JSON字符串到datatable jquery

json string render:

'[{"COQUANBANHANH_ID":89,"VANBANDI_ID":null,"THOIHAN_XULY":null,"StrVanBanLienQuan":null,"COQUANBANHANHTEXT":null,"DOKHAN_ID":1,"DOKHAN":"Thường","DOMAT_ID":null,"DONVINHANHOSO_ID":null,"GHICHU":null,"HAS_FILE":null,"HOSO_ID":null,"ID":318,"IS_DELETE":null,"LAPHOSO_YN":null,"LOAIVANBAN_ID":3,"LOAIVANBAN":"Công Văn","MATRANGTHAI":null,"NGAYCAPNHAT":null,"NGAYDEN":"2017-03-22T01:24:07.197","NGAYHETHIEULUC":null,"NGAYTAO":null,"NGAYVANBAN":"2017-03-22T00:00:00","NGUOICAPNHAT":null,"NGUOIGIU":null,"NGUOIKY":null,"NGUOITAO":23,"NHOMSOVANBAN_ID":3,"SODEN":12,"SOKYHIEU":"12/CV-CNTT","SOTRANG":null,"SOVANBANDEN_ID":18,"TENTRANGTHAI":null,"THOIHANXULY_BATDAU":null,"THOIHANXULY_KETTHUC":null,"TIEUDE":null,"TRANGTHAI":1,"TRICHYEU":"Hướng dẫn ban hành văn bản đi","WF_ID":null,"WF_TRANGTHAI_ID":1,"DONVIBANHANH":"Công ty HiNet","IS_FAVORITE":null,"IS_IMPORTANT":null,"IS_READ":true,"NOIDUNGVANBAN":null,"SOBANSAO":null,"TRANGTHAI_XULY":null,"NGUOI_XULY_CHINH":null,"IS_NOIBO":true,"IS_CUNGHETHONG":null,"ListSteps":null,"IS_LANHDAO_READ":false,"HAS_ROLE_COMMENT":false},{"COQUANBANHANH_ID":1,"VANBANDI_ID":null,"THOIHAN_XULY":null,"StrVanBanLienQuan":null,"COQUANBANHANHTEXT":null,"DOKHAN_ID":1,"DOKHAN":"Thường","DOMAT_ID":null,"DONVINHANHOSO_ID":null,"GHICHU":null,"HAS_FILE":false,"HOSO_ID":null,"ID":317,"IS_DELETE":false,"LAPHOSO_YN":null,"LOAIVANBAN_ID":1,"LOAIVANBAN":"Báo cáo","MATRANGTHAI":null,"NGAYCAPNHAT":null,"NGAYDEN":"2017-03-22T00:00:00","NGAYHETHIEULUC":null,"NGAYTAO":"2017-03-22T00:05:00.103","NGAYVANBAN":"2017-03-22T00:00:00","NGUOICAPNHAT":null,"NGUOIGIU":null,"NGUOIKY":null,"NGUOITAO":32,"NHOMSOVANBAN_ID":3,"SODEN":33,"SOKYHIEU":"CV/01-HN2017","SOTRANG":null,"SOVANBANDEN_ID":18,"TENTRANGTHAI":null,"THOIHANXULY_BATDAU":null,"THOIHANXULY_KETTHUC":null,"TIEUDE":null,"TRANGTHAI":4,"TRICHYEU":"Quy định kê khai thuế năm 2017","WF_ID":null,"WF_TRANGTHAI_ID":4,"DONVIBANHANH":"CƠ QUAN NHÀ NƯỚC","IS_FAVORITE":true,"IS_IMPORTANT":null,"IS_READ":true,"NOIDUNGVANBAN":null,"SOBANSAO":null,"TRANGTHAI_XULY":null,"NGUOI_XULY_CHINH":null,"IS_NOIBO":null,"IS_CUNGHETHONG":null,"ListSteps":null,"IS_LANHDAO_READ":false,"HAS_ROLE_COMMENT":false}]'

并且震惊了数据:

 var table = $('.wtfayo').DataTable({
            "sDom": "<'dt-toolbar'<'col-xs-12 col-sm-6'f><'col-sm-6 col-xs-12 hidden-xs'l>r>" +
                "t" +
                "<'dt-toolbar-footer'<'col-sm-6 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-6'p>>",
            "data": JSON.parse('@JsonData'),
            "bDestroy": true,
            "iDisplayLength": 15,
            "columns": [
                {
                    "class": 'details-control',
                    "orderable": false,
                    "data": null,
                    "defaultContent": ''
                },
                { "data": "TRICHYEU" },

            ],
            "order": [[1, 'asc']],
            "fnDrawCallback": function (oSettings) {
                runAllCharts()
            }
        });

但JSON.parse无法正常工作

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

<强>控制器/ ReportController.cs

public class ReportController : Controller
{
    public ActionResult Report()
    {
        var peaples = new[]
        {
            new Person { Name = "lisa", Family = "conner" },
            new Person { Value = "jan", Family = "conner" }
        };

        var model = new ReportModel
        {
            Peaples = peaples
        };

        return View(model);
    }
}

<强>模型/ ReportModel.cs

public class ReportModel
{
    public IEnumerable<Person> Peaples { get; set; }
}

<强>查看/报告/ Report.cshtml

@model MyApp.Models.ReportModel

<script>
    var peaples = @Html.Raw(Json.Encode(Model.Peaples));
</script>