反序列化通用List<>中的字符串用双引号爆炸

时间:2014-12-02 15:54:39

标签: c# asp.net json

当尝试在本地存储中检索并存储序列化数据列表然后发送回服务器以进行反序列化为其原始格式时,当对象中的字符串包含转义的双精度时,该过程会爆炸报价。

如果对象不在集合中(我已尝试过List和ArrayObject),则代码可以正常工作。什么是导致悲伤的集合?

有人可以告诉我这里我做错了什么,或建议一个更好的方法来达到相同的结果。我在尝试创建离线Web应用程序时遇到了这个问题。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="_default" %>
        <!DOCTYPE html>
        <html lang="en">
        <body>
        <form runat="server">
            <div id="Feedback"></div>
            <div><a href="#" id="ExportButton">Export</a></div>
            <div><a href="#" id="ImportButton">Import</a></div>
        </form>
        <script src="js/jquery-2.1.1.min.js"></script>
        <script>
        function Export() {
            $.ajax({
                type: "POST",
                url: 'default.aspx/GetThings',
                contentType: "application/json; charset=u",
                dataType: "json",
                async: true,
                success: function (result) {
                    var json = result.d;

                    $('#Feedback').html(json);

                    // failed attempt #1
                    //localStorage["things"] = json;

                    // failed attempt #2
                    var things = jQuery.parseJSON(json);
                    localStorage["things"] = JSON.stringify(things);
                }
            });
        }

        function Import() {
            $.ajax({
                type: "POST",
                url: "default.aspx/PutThings",
                data: "{'json':'" + localStorage["things"] + "'}",
                contentType: "application/json; charset=u",
                dataType: "json",
                async: true,
                success: function (result) {
                    $('#Feedback').html(result.d);
                }
            });
        }

        $(function () {
            $("#ExportButton").click(function () {
                Export();
            });

            $("#ImportButton").click(function () {
                Import();
            });
        });
        </script>
        </body>
        </html>


using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI;

public partial class _default : Page
{
    [WebMethod]
    public static string GetThings()
    {
        List<Thing> things = new List<Thing>();

        Thing thing = new Thing();
        //thing.Description = "no escaped double quote string works fine";
        thing.Description = "100 (2 1/8\") Gear";
        things.Add(thing);

        JavaScriptSerializer serializer = new JavaScriptSerializer();

        StringBuilder json = new StringBuilder();

        serializer.Serialize(things, json);

        return json.ToString();
    }

    [WebMethod]
    public static string PutThings(string json)
    {
        try
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();

            List<Thing> things = serializer.Deserialize<List<Thing>>(json);

            return things[0].Description;
        }
        catch (Exception e)
        {
            return e.Message;
        }
    }

    public class Thing
    {
        [DataMember]
        public string Description { get; set; }
    }
}

1 个答案:

答案 0 :(得分:0)

PutThings()方法的顶部放置一个断点,希望您有充分的理由手动序列化/反序列化,而不仅仅是letting WCF take care of this for you

当您获得断点时,您可以查看传递给服务的JSON字符串,您将能够自己判断需要什么样的转换才能使其成为有效的JSON字符串。我猜这是发送到此函数的数据字符串的构建,因为它看起来像是在子JSON对象字符串localStorage["things"]周围放置引号。代码应该是..

data: "{'json':" + localStorage["things"] + "}",

如果没有,请发送传递给您的Put函数的JSON字符串,我将能够给您一个更准确的答案。