Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 反序列化泛型列表中的字符串<&燃气轮机;用双引号爆炸_C#_Asp.net_Json - Fatal编程技术网

C# 反序列化泛型列表中的字符串<&燃气轮机;用双引号爆炸

C# 反序列化泛型列表中的字符串<&燃气轮机;用双引号爆炸,c#,asp.net,json,C#,Asp.net,Json,当试图在本地存储中检索并存储序列化的数据列表,然后将其发送回服务器以反序列化为其原始形式时,当对象中的字符串包含转义双引号时,该进程将爆炸 如果对象不在集合中(我尝试了List和ArrayObject),代码工作正常。一个会引起悲伤的收藏是什么 有人能告诉我我做错了什么,或者建议一个更好的方法来达到同样的结果吗。我在尝试创建脱机web应用程序时遇到了这个问题 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.as

当试图在本地存储中检索并存储序列化的数据列表,然后将其发送回服务器以反序列化为其原始形式时,当对象中的字符串包含转义双引号时,该进程将爆炸

如果对象不在集合中(我尝试了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; }
    }
}

函数导出(){
$.ajax({
类型:“POST”,
url:'default.aspx/GetThings',
contentType:“application/json;charset=u”,
数据类型:“json”,
async:true,
成功:功能(结果){
var json=result.d;
$('#Feedback').html(json);
//尝试失败#1
//localStorage[“things”]=json;
//尝试失败#2
var things=jQuery.parseJSON(json);
localStorage[“things”]=JSON.stringify(things);
}
});
}
函数导入(){
$.ajax({
类型:“POST”,
url:“default.aspx/PutThings”,
数据:“{'json':'”+localStorage[“things”]+“}”,
contentType:“application/json;charset=u”,
数据类型:“json”,
async:true,
成功:功能(结果){
$('#Feedback').html(result.d);
}
});
}
$(函数(){
$(“#导出按钮”)。单击(函数(){
出口();
});
$(“#导入按钮”)。单击(函数(){
导入();
});
});
使用制度;
使用System.Collections.Generic;
使用System.Runtime.Serialization;
使用系统文本;
使用System.Web.Script.Serialization;
使用System.Web.Services;
使用System.Web.UI;
公共部分类\u默认值:第页
{
[网络方法]
公共静态字符串GetThings()
{
列出事物=新列表();
事物=新事物;
//thing.Description=“无转义双引号字符串工作正常”;
thing.Description=“100(21/8)”档位;
事物。添加(事物);
JavaScriptSerializer serializer=新的JavaScriptSerializer();
StringBuilder json=新的StringBuilder();
serializer.Serialize(事物,json);
返回json.ToString();
}
[网络方法]
公共静态字符串PutThings(字符串json)
{
尝试
{
JavaScriptSerializer serializer=新的JavaScriptSerializer();
List things=serializer.Deserialize(json);
返回物品[0]。说明;
}
捕获(例外e)
{
返回e.消息;
}
}
公共类事物
{
[数据成员]
公共字符串说明{get;set;}
}
}

PutThings()
方法的顶部放置一个断点,希望您有一个很好的理由手动序列化/反序列化,而不只是手动

当您获得断点后,您可以查看传递给服务的JSON字符串,您可以自己告诉自己需要什么样的转换才能将其转换为有效的JSON字符串。我猜这是发送到此函数的数据字符串的构建,因为它看起来像是在子JSON obj周围加引号ect字符串
localStorage[“things”]
。代码应为

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

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

explodes=what?你能包含异常信息吗?返回JSON.ToString()是否必要?如果你像标签建议的那样使用asp.net,还有其他(更好的)方法返回json的方法。请先尝试此方法,看看问题是否仍然存在。传入的对象无效,应为“:”或“}”。(29):[{“Description:“100(2 1/8”)Gear”}]我尝试使用更流行的json.NET而不是本机JavaScriptSerializer进行序列化/反序列化,遗憾的是,我得到了完全相同的结果。这让我相信问题出在我的代码上,而不是序列化程序。另外,仅供参考,严格来说,JSON在参数和字段名周围使用双引号而不是单引号。这就是发送到服务的内容:[{“Description”:“100(2 1/8”)Gear”}]建议的数据参数代码无效,ajax调用失败。因此
JSON.stringify(things);
的结果是
“100(21/8”)齿轮”
?这似乎不对,JSON应该是有效的,因为我可以看到您的反序列化和重新序列化,如果JSON无效,这将引发错误,而且该字符串应该存储在数组中的Description子属性中。我发现了另一个错误,
JavaScriptSerializer.Serialize(…)
返回JSON字符串,
JavaScriptSerializer.ToString()
不应: