简体   繁体   English

C#使用JavaScriptSerializer用字符'/'反序列化JSON

[英]C# Deserialize JSON with chars '/' using JavaScriptSerializer

I've got such JSON string: 我有这样的JSON字符串:

 "[{\"macId\":\"ttsh\",\"modelNumber\":\"tshf\"},{\"macId\":\"dtgstr\",\"modelNumber\":\"drtgsdsrtg\"}]"

I want to Deserialize it. 我想反序列化它。 What I'm doing: 我在做什么:

    private static IList<HandsetModel> handsetsList = new List<HandsetModel>();
    handsetsList = new JavaScriptSerializer().Deserialize<List<HandsetModel>>(handsetsJson);

public sealed class HandsetModel
{
    public string macId { get; set; }

    public string modelNumber { get; set; }
}

What I've tryed to do with string: 我尝试对字符串进行的操作:

handsetsJson = handsetsJson.Replace(@"\", "");

But this line didn't help me. 但是这条线对我没有帮助。 May anybody help? 有人可以帮忙吗?

From memory, I believe there are several things you can do to clean this up: 从内存来看,我相信您可以做一些事情来清理它:

One route is something like: 一种路线是这样的:

 public static string CleanAllWhiteSpace(string messyJson)
        {
            return (Regex.Replace(messyJson, @"(\s)+|(\n)+|(\r)+|(\t)+", ""));
        }

        public static string removeEscapedQuotes(string escapedString)
        {
            return (Regex.Replace(escapedString, "\\\"", "\""));
        }

Another thought, again from memory is use NewtonSoft.JSON instead of the .NET native implementation as it seems like it serialized/ deserialized more cleanly/ predictably. 从内存中再次想到的另一种想法是使用NewtonSoft.JSON而不是.NET本机实现,因为它看起来更清晰/可预测地进行了序列化/反序列化。

For example with NewtonSoft's: 例如,NewtonSoft的:

string serial = JsonConvert.SerializeObject(s).ToString();

I get a nice clean: 我得到一个很好的清洁:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM