本文介绍了返回JSON,但它包含反斜杠" \\&QUOT ;,这是我不希望的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用MVC4 Web的API,C#,并希望返回JSON的 uning Json.net。

问题是它自带的反斜杠。

我还添加了这code到Global.asax中。

  GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();

下面就是它的回报。

<$p$p><$c$c>\"{\\\"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}\"

所以,我希望看到的是

<$p$p><$c$c>{\"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}

下面是JsonConvetor

  JSON字符串= JsonConvert.SerializeObject(MyObj中);


解决方案

我发现这里的解决方案是

 返回新的Htt presponseMessage()
        {
            内容=新的StringContent(JSON,System.Text.Encoding.UTF8,应用/ JSON)
        };

I use MVC4 web-api, c#, and want to return Json uning Json.net.

the problem is it comes with "backward slashes".

I also added this code to Global.asax.

GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();

here is what it returns.

"{\"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}"

so what i want to see is

{"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}

here is JsonConvetor

string json = JsonConvert.SerializeObject(myObj);
解决方案

i found the solution here it is

return new HttpResponseMessage()
        {
            Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json")
        };

这篇关于返回JSON,但它包含反斜杠&QUOT; \\&QUOT ;,这是我不希望的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 00:56