ris*_*res 37 c# json asp.net-mvc-4 asp.net-web-api
我使用MVC4 web-api,c#,并希望使用Json.net返回Json .
问题是它带有"反斜杠".
我还将此代码添加到Global.asax.
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
Run Code Online (Sandbox Code Playgroud)
这是它的回报.
"{\"cid\":1,\"model\":\"WT50JB\",\"detail\":\"sdf??\",\"unit\":2,\"time_in\":\"2012-12-11T19:00:00\",\"time_out\":\"2012-12-12T13:00:06.2774691+07:00\",\"time_used_dd\":0.0,\"time_used_hh\":0.0}"
Run Code Online (Sandbox Code Playgroud)
所以我想看到的是
{"cid":1,"model":"WT50JB","detail":"sdf??","unit":2,"time_in":"2012-12-11T19:00:00","time_out":"2012-12-12T13:08:50.5444555+07:00","time_used_dd":0.0,"time_used_hh":0.0}
Run Code Online (Sandbox Code Playgroud)
这是JsonConvertor
string json = JsonConvert.SerializeObject(myObj);
Run Code Online (Sandbox Code Playgroud)
id.*_*.ot 30
我有同样的问题,直到刚才.事实证明我是"双序列化"JSON字符串.我使用jQuery $.getJson(
AJAX调用JsonResult
控制器动作.并且因为该操作构建了一个C#Generic,List<t>
我认为List<t>
在使用以下内容返回JSON之前,我必须使用JSON.net/NewtonSoft将C#Generic 转换为JSON对象:
return Json(fake, JsonRequestBehavior.AllowGet);
Run Code Online (Sandbox Code Playgroud)
毕竟我没有必要使用这个JsonConvert.SerializeObject(
方法,显然这return
将为我们收集序列化.
希望它也可以帮助你或其他人.
ris*_*res 24
我在这里找到了解决方案
return new HttpResponseMessage()
{
Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json")
};
Run Code Online (Sandbox Code Playgroud)
小智 11
using Newtonsoft.Json.Linq;
string str = "Your String with Back Slashes";
str = JToken.Parse(str).ToString(); `// Now You will get the Normal String with "NO SLASHES"`
Run Code Online (Sandbox Code Playgroud)
Cod*_*odo 10
最有可能的是,斜杠是一个工件,因为您将它们从VisualStudio调试器中复制出来.调试器以可以粘贴到C/C#代码中的方式显示所有字符串.它们实际上并不在传输数据中.
BTW:这些斜杠是落后的斜线.正斜杠看起来像这样:/.
归档时间: |
|
查看次数: |
60887 次 |
最近记录: |