使用JSON.NET返回的ActionResult [英] Using JSON.NET to return ActionResult

查看:258
本文介绍了使用JSON.NET返回的ActionResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个C#方法,将序列化的模型,并返回一个JSON结果。这里是我的code:

 公众的ActionResult读([DataSourceRequest] DataSourceRequest要求)
    {
        变种项= db.Words.Take(1).ToList();
        JsonSerializerSettings jsSettings =新JsonSerializerSettings();
        jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        VAR转换= JsonConvert.SerializeObject(项目,空,jsSettings);
        返回JSON(换算,JsonRequestBehavior.AllowGet);
    }

我得到了以下JSON结果时,我去字/读取Chrome浏览器:

<$p$p><$c$c>\"[{\\\"WordId\\\":1,\\\"Rank\\\":1,\\\"PartOfSpeech\\\":\\\"article\\\",\\\"Image\\\":\\\"Upload/29/1/Capture1.PNG\\\",\\\"FrequencyNumber\\\":\\\"22038615\\\",\\\"Article\\\":null,\\\"ClarificationText\\\":null,\\\"WordName\\\":\\\"the | article\\\",\\\"MasterId\\\":0,\\\"SoundFileUrl\\\":\\\"/UploadSound/7fd752a6-97ef-4a99-b324-a160295b8ac4/1/sixty_vocab_click_button.mp3\\\",\\\"LangId\\\":1,\\\"CatId\\\":null,\\\"IsActive\\\":false}

我觉得\\转义引号是当您双击序列化一个对象从其他问题时出现的问题:
<一href=\"http://stackoverflow.com/questions/22294473/wcf-json-output-is-getting-unwanted-quotes-backslashes-added\">WCF JSON输出是收到骚扰报价和放大器;反斜线添加

这肯定看起来像我双序列化我的对象,因为我第一次使用序列化和JSON.NET然后我的结果传递到JSON()函数。我需要手动序列化,以避免referenceloops,但我觉得我需要查看一个ActionResult。

我如何在这里返回一个ActionResult?我是否需要,或者我只返回一个字符串?


解决方案

我发现了一个类似计算器的问题:
JSon.net而ActionResult的

答案有使用提示

 返回内容(改装后的应用/ JSON);

这似乎是我最简单的网页上工作。

I'm trying to write a C# method that will serialize a model and return a JSON result. Here's my code:

    public ActionResult Read([DataSourceRequest] DataSourceRequest request)
    {
        var items = db.Words.Take(1).ToList();
        JsonSerializerSettings jsSettings = new JsonSerializerSettings();
        jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        var converted = JsonConvert.SerializeObject(items, null, jsSettings);
        return Json(converted, JsonRequestBehavior.AllowGet);
    }

I got the following JSON result when I go to Words/Read in Chrome:

"[{\"WordId\":1,\"Rank\":1,\"PartOfSpeech\":\"article\",\"Image\":\"Upload/29/1/Capture1.PNG\",\"FrequencyNumber\":\"22038615\",\"Article\":null,\"ClarificationText\":null,\"WordName\":\"the | article\",\"MasterId\":0,\"SoundFileUrl\":\"/UploadSound/7fd752a6-97ef-4a99-b324-a160295b8ac4/1/sixty_vocab_click_button.mp3\",\"LangId\":1,\"CatId\":null,\"IsActive\":false}

I think the \" escaped quotes are a problem that occurs when you double serialize an object. From other questions: WCF JSON output is getting unwanted quotes & backslashes added

It definitely looks like I'm double serializing my object, since I first serialize using JSON.NET and then pass my result into the Json() function. I need to manually serialize to avoid referenceloops, but I think my View needs an ActionResult.

How can I return an ActionResult here? Do I need to, or can I just return a string?

解决方案

I found a similar stackoverflow question: JSon.net And ActionResult

The answer there suggested using

return Content( converted, "application/json" );

That seems to work on my very simple page.

这篇关于使用JSON.NET返回的ActionResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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