Json.NET:使用双引号进行反序列化 [英] Json.NET: Deserilization with Double Quotes

查看:194
本文介绍了Json.NET:使用双引号进行反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试反序列化从服务收到的作为响应的json字符串.客户端是Windows Phone 7(使用C#).我正在使用 Json .NET-James Newton-King 反序列化器直接转换Json字符串到对象.但是有时Json字符串中包含一些注释信息,并在其中加上双引号(),并且反序列化程序失败并引发错误.根据 Jsonlint .

I am trying to deserialize a json string received as a response from the service. The client is Windows Phone 7, in C#. I am using Json .NET - James Newton-King deserializor to directly convert the Json string to objects. But sometimes the Json string contains some comments information with double quotes (") in them and the deserializer fails and throws an error. Looks like this is an invalid Json string according to Jsonlint.

{
    "Name": "A1",
    "Description": "description of the "object" A1"
}

如何处理这样的Json String.如果它是(\),那么它可以工作.但是我无法用(\")替换所有(),​​因为json字符串的其他部分可能会有双引号.是否有Json .Net的任何解码功能?

How to handle such Json String. If it is (\"), then it works. But I cannot replace all (") with (\") as there might be double quotes in other part of the json string. Is there any decode function of Json .Net?

推荐答案

看起来像HttpUtility.JavaScriptStringEncode可能会解决您的问题.

It looks like HttpUtility.JavaScriptStringEncode might solve your issue.

HttpUtility.JavaScriptStringEncode(JsonConvert.SerializeObject(yourObject))

这篇关于Json.NET:使用双引号进行反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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