我正在尝试编写一个C#方法,它将序列化一个模型并返回一个JSON结果.这是我的代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
当我转到Chrome中的Words/Read时,我得到了以下JSON结果:
"[{\"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}
Run Code Online (Sandbox Code Playgroud)
我认为\'转义引号是双重序列化对象时出现的问题.从其他问题: WCF JSON输出添加了不需要的引号和反斜杠
它看起来好像是我对我的对象进行双重序列化,因为我首先使用JSON.NET进行序列化,然后将我的结果传递给Json()函数.我需要手动序列化以避免引用循环,但我认为我的View需要一个ActionResult.
我怎样才能在这里返回一个ActionResult?我需要,还是只能返回一个字符串?