asp.net JSON序列添加反斜线" \\"我的属性 [英] asp.net json serializer adding backslash "\" to my properties

查看:137
本文介绍了asp.net JSON序列添加反斜线" \\"我的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样一个对象模型:

I have an object model that looks like this:

public class MyObjectModel
{
  public int1 {get;set;}
  public int2 {get;set;}

  [ScriptIgnore]
  public int3 {get;set;}
}

在我的code,我写这篇文章:

In my code, I write this:

MyObjectModel TheObject  = new MyObjectModel();

TheObject = LoadFromQuery(); //populates the properties using a linq-to-sql query

JavaScriptSerializer MyObjectSerializer = new JavaScriptSerializer();

string TheObjectInJson = MyObjectSerializer.Serialize(TheObject);

当我看着JSON字符串TheObjectInJson,它找出来是这样的:

When I look at the json string TheObjectInJson, it comes out looking like this:

"{\"int1\":31,\"int2\":5436}"

串行器增加了一个反斜杠每个属性。我尝试添加和删除上面的类定义,但无济于事[Serializable]属性。

The serializer adds a backslash to each property. I tried adding and removing the [Serializable] attribute above the class definition but to no avail.

任何建议,为什么会这样?

Any suggestions why this is happening?

感谢。

推荐答案

这应该是正确的。当发送JSON返回给浏览器,所有属性名必须在引号。您所看到的反斜线Visual Studio中查看它们(我希望,你,当你看到这并没有提及)。

That should be correct. When sending JSON back to the browser, all property names must be in quotes. The backslashes you see are Visual Studio escaping the strings when viewing them (I hope, you didn't mention when you're seeing this).

如果您实际发送的数据进行回线路上它应该给人的印象

If you actually send that data back out on the wire it should come across as

{"int1": 31, "int2":5436}

这是正确的JSON符号。

which is proper JSON notation.

请参阅维基百科以JSON表示法的一个例子。

See Wikipedia for an example of JSON notation.

这篇关于asp.net JSON序列添加反斜线" \\"我的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆