使用JsonResult返回时,JSON对象被序列化为大括号 [英] JSON Objects are serialized to empty brackets when returned using JsonResult

查看:349
本文介绍了使用JsonResult返回时,JSON对象被序列化为大括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET MVC 5在.NET Framework中进行操作.我有一个MVC控制器,该控制器正在使用Newtonsoft.Json(或json.net)来构建漂亮的JSON对象.我遇到的麻烦是,当我使用JsonResult方法Json(JSONdotnetObject)返回json时,最终得到的结果与我期望的结构相同,但是所有内容都是空的JArrays,如下所示:

I am operating in the .NET Framework using ASP.NET MVC 5. I have an MVC Controller that is using Newtonsoft.Json (or json.net) to build a nice JSON object. The trouble I'm running into is that when I return my json using the JsonResult method Json(JSONdotnetObject), I end up with a result that has the same structure as what I would expect but everything is empty JArrays like so:

预期:

{
    "prop": {
        ... some stuff ...
     },
     "another prop": {
         ... more stuff ...
     }
}

实际:

[
    [
        ... empty ...
    ],
    [
        ... also empty ...
    ]
]

这是代码:

public ActionResult methodName(string input) {
    JObject myJSON = new JObject();

    // Populate myJSON with children
    foreach (Thing thing in ExistingEntityFrameworkCustomClass) {
        JObject newChild = new JObject();

        // Add some props to newChild using newChild["propName"] = thing.Property

        myJSON.Add(newChild.UniqueIdTakenFromEntity.ToString(), newChild);
    }

    // The JsonResult successfully populates it's Data field with myJSON before returning.
    return Json(myJSON, JsonRequestBehavoir.AllowGet); 
}

这不是确切的结构,但希望它能说明我正在经历的事情.我已经在VS中逐行浏览了程序,并且JsonResult对象正确地将JObject保留在JsonResult.Data字段中,但是当它返回时,ASP.NET执行的序列化会出错,这会导致我失去了我美丽的JSON对象:*(

That's not the exact structure but hopefully it illustrates what I'm experiencing. I've gone through the program line by line in VS and the JsonResult object correctly holds the JObject in the JsonResult.Data field, but when it returns something goes wrong with the serialization that ASP.NET carries out that is causing me to lose my beautiful JSON object :*(

我已经通过仅从方法中返回JObject而不是ActionResult来解决此问题,但这并不理想,因为然后我不得不使用Result.ContentType手动设置响应标头,感觉就像是被黑客入侵了

I have worked around this issue by simply returning a JObject instead of an ActionResult from the method but this isn't ideal because I then have to manually set the response headers using Result.ContentType which feels like a hack.

如果您有任何想法或可以使用更多细节,请与我联系.谢谢您的时间.

Let me know if you have any ideas or could use more detail. Thank you for your time.

推荐答案

问题是MVC Controller类中的Json方法在内部使用了JavaScriptSerializer(不是Json.Net),并且不知道如何序列化JObject正确.尝试改用Content方法:

The problem is that the Json method in the MVC Controller class uses JavaScriptSerializer internally (not Json.Net), and does not know how to serialize a JObject properly. Try using the Content method instead:

public ActionResult methodName(string input) {
    JObject myJSON = new JObject();

    // Populate myJSON with children

    return Content(myJSON.ToString(), "application/json", Encoding.UTF8); 
}

或者,您可以实现自己的JsonNetResult类,如James Newton-King的博客文章

Alternatively, you can implement your own JsonNetResult class as shown in James Newton-King's blog article, ASP.NET MVC and Json.NET.

这篇关于使用JsonResult返回时,JSON对象被序列化为大括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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