将元组序列化为JSON

时间:2017-06-17 02:34:20

标签: c# jquery json ajax asp.net-mvc

尝试将元组序列化为json:

List<Tuple<string, string, string, string, string, string>> iCalEvents = new List<Tuple<string, string, string, string, string, string>>();

如果我使用:

输出值
string iCalEventsJson = JsonConvert.SerializeObject(iCalEvents);

它产生有效的json:

  

[{“Item1”:“Film / Event - 05/10”,“Item2”:“”,“Item3”:“”,“Item4”:“Lorem   ipsum dolor sit amet,consectetur adipisicing elit。 Saepe harum,   similique eaque ratione optio nihil est,nostrum velit minima soluta,   recusandae quis,tenetur sunt ut。 Tempora accusamus provident maxime,   sunt debitis quaerat nemo,nostrum cupiditate necessitatibus ipsam   Deleniti consequatur quos quae odio amet obcaecati,   eligendi。“,”Item5“:”2017-05-10“,”Item6“:”2017-05-12“},{”Item1“:”电影/   活动详情 - 05/12“,”Item2“:”“,”Item3“:”“,”Item4“:”发展电影   /事件   详情“,”Item5“:”2017-05-12“,”Item6“:”2017-05-12“},{”Item1“:”电影/   活动详情 - 05/14“,”Item2“:”“,”Item3“:”“,”Item4“:”发展电影   /事件   详情“,”Item5“:”2017-05-14“,”Item6“:”2017-05-14“},{”Item1“:”电影/   活动详情 - 05/16“,”Item2“:”“,”Item3“:”“,”Item4“:”开发电影   /事件   详情“,”Item5“:”2017-05-16“,”Item6“:”2017-05-16“},{”Item1“:”电影/   活动详情 - 05/19“,”Item2“:”“,”Item3“:”“,”Item4“:”开发电影   / Event Detail“,”Item5“:”2017-05-19“,”Item6“:”2017-05-19“}]

但是,当我将字符串变量传递给jquery ajax时,如下所示:

var json = @iCalEventsJson;    
$.ajax({
        url: '@Url.Action("CreateICalEvent", "ICalEvent")',
        type: 'POST',
        dataType: 'json',
        data: json,
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            var message = data.Message;
            $("#resultMessage").html(message);
        }
    })

它产生无效的json并导致ajax失败:

[{&quot;Item1&quot;:&quot;Film / Event -- 05/10&quot;,&quot;Item2&quot;:&quot;&quot;,&quot;Item3&quot;:&quot;&quot;,&quot;Item4&quot;:&quot;Lorem ipsum dolor sit amet, consectetur adipisicing elit. Saepe harum, similique eaque ratione optio nihil est, nostrum velit minima soluta, recusandae quis, tenetur sunt ut. Tempora accusamus provident maxime, sunt debitis quaerat nemo, nostrum cupiditate necessitatibus ipsam deleniti consequatur quos quae odio amet obcaecati, eligendi.&quot;,&quot;Item5&quot;:&quot;2017-05-10&quot;,&quot;Item6&quot;:&quot;2017-05-12&quot;},{&quot;Item1&quot;:&quot;Film / Event Detail -- 05/12&quot;,&quot;Item2&quot;:&quot;&quot;,&quot;Item3&quot;:&quot;&quot;,&quot;Item4&quot;:&quot;Development Film / Event Detail&quot;,&quot;Item5&quot;:&quot;2017-05-12&quot;,&quot;Item6&quot;:&quot;2017-05-12&quot;},{&quot;Item1&quot;:&quot;Film / Event Detail -- 05/14&quot;,&quot;Item2&quot;:&quot;&quot;,&quot;Item3&quot;:&quot;&quot;,&quot;Item4&quot;:&quot;Development Film / Event Detail&quot;,&quot;Item5&quot;:&quot;2017-05-14&quot;,&quot;Item6&quot;:&quot;2017-05-14&quot;},{&quot;Item1&quot;:&quot;Film / Event Detail -- 05/16&quot;,&quot;Item2&quot;:&quot;&quot;,&quot;Item3&quot;:&quot;&quot;,&quot;Item4&quot;:&quot;Development Film / Event Detail&quot;,&quot;Item5&quot;:&quot;2017-05-16&quot;,&quot;Item6&quot;:&quot;2017-05-16&quot;},{&quot;Item1&quot;:&quot;Film / Event Detail -- 05/19&quot;,&quot;Item2&quot;:&quot;&quot;,&quot;Item3&quot;:&quot;&quot;,&quot;Item4&quot;:&quot;Development Film / Event Detail&quot;,&quot;Item5&quot;:&quot;2017-05-19&quot;,&quot;Item6&quot;:&quot;2017-05-19&quot;}]

还试过编码:

var json = @HttpUtility.HtmlEncode(iCalEventsJson);
$.ajax({
    url: '@Url.Action("CreateICalEvent", "ICalEvent")',
    type: 'POST',
    dataType: 'json',
    data: json,
    contentType: 'application/json; charset=utf-8',
    success: function (data) {
        var message = data.Message;
        $("#resultMessage").html(message);
    }
});

......但json仍然无效:

[{&amp;quot;Item1&amp;quot;:&amp;quot;Film / Event -- 05/10&amp;quot;,&amp;quot;Item2&amp;quot;:&amp;quot;&amp;quot;,&amp;quot;Item3&amp;quot;:&amp;quot;&amp;quot;,&amp;quot;Item4&amp;quot;:&amp;quot;Lorem ipsum dolor sit amet, consectetur adipisicing elit. Saepe harum, similique eaque ratione optio nihil est, nostrum velit minima soluta, recusandae quis, tenetur sunt ut. Tempora accusamus provident maxime, sunt debitis quaerat nemo, nostrum cupiditate necessitatibus ipsam deleniti consequatur quos quae odio amet obcaecati, eligendi.&amp;quot;,&amp;quot;Item5&amp;quot;:&amp;quot;2017-05-10&amp;quot;,&amp;quot;Item6&amp;quot;:&amp;quot;2017-05-12&amp;quot;},{&amp;quot;Item1&amp;quot;:&amp;quot;Film / Event Detail -- 05/12&amp;quot;,&amp;quot;Item2&amp;quot;:&amp;quot;&amp;quot;,&amp;quot;Item3&amp;quot;:&amp;quot;&amp;quot;,&amp;quot;Item4&amp;quot;:&amp;quot;Development Film / Event Detail&amp;quot;,&amp;quot;Item5&amp;quot;:&amp;quot;2017-05-12&amp;quot;,&amp;quot;Item6&amp;quot;:&amp;quot;2017-05-12&amp;quot;},{&amp;quot;Item1&amp;quot;:&amp;quot;Film / Event Detail -- 05/14&amp;quot;,&amp;quot;Item2&amp;quot;:&amp;quot;&amp;quot;,&amp;quot;Item3&amp;quot;:&amp;quot;&amp;quot;,&amp;quot;Item4&amp;quot;:&amp;quot;Development Film / Event Detail&amp;quot;,&amp;quot;Item5&amp;quot;:&amp;quot;2017-05-14&amp;quot;,&amp;quot;Item6&amp;quot;:&amp;quot;2017-05-14&amp;quot;},{&amp;quot;Item1&amp;quot;:&amp;quot;Film / Event Detail -- 05/16&amp;quot;,&amp;quot;Item2&amp;quot;:&amp;quot;&amp;quot;,&amp;quot;Item3&amp;quot;:&amp;quot;&amp;quot;,&amp;quot;Item4&amp;quot;:&amp;quot;Development Film / Event Detail&amp;quot;,&amp;quot;Item5&amp;quot;:&amp;quot;2017-05-16&amp;quot;,&amp;quot;Item6&amp;quot;:&amp;quot;2017-05-16&amp;quot;},{&amp;quot;Item1&amp;quot;:&amp;quot;Film / Event Detail -- 05/19&amp;quot;,&amp;quot;Item2&amp;quot;:&amp;quot;&amp;quot;,&amp;quot;Item3&amp;quot;:&amp;quot;&amp;quot;,&amp;quot;Item4&amp;quot;:&amp;quot;Development Film / Event Detail&amp;quot;,&amp;quot;Item5&amp;quot;:&amp;quot;2017-05-19&amp;quot;,&amp;quot;Item6&amp;quot;:&amp;quot;2017-05-19&amp;quot;}]

我错过了什么?

1 个答案:

答案 0 :(得分:2)

你可能想要使用Html.Raw或者现在的等价物。

var json = @Html.Raw(iCalEventsJson);