当我试图序列化一个包含斜杠/
的字符串属性的对象时,JSON生成的每个斜杠都会转义两次。带双转义字符的序列化JSON
实施例:
随机类:
public class Foo
{
[DataMember(Name = "bar"]
public string Bar { get; set; }
}
而且
Foo foo = new Foo() { Bar = " Foo/Bar" };
string json = RandomStaticClass.Serialize(foo);
的JSON将是:
{
\"bar\":\"Foo \\/ Bar\"
}
导致:
{
"bar":"Foo \/ Bar"
}
虽然,我只是想:
{
"bar":"Foo/Bar"
}
任何想法?谢谢:)
这里是我的功能序列化对象:
public static string Serialize(object instance)
{
using (MemoryStream stream = new MemoryStream())
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(instance.GetType());
serializer.WriteObject(stream, instance);
stream.Position = 0;
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
序列化程序执行此操作,但没有关闭它的选项。这是有效的JSON,所以它有一个被逃脱的问题? – 2015-01-21 10:53:55
问题是斜线被转了两次。 – ShuuZ 2015-01-21 10:55:53