如何返回字典作为 JsonResult,并在 JavaScript 中获得正确的结果?

发布于 2024-10-04 07:59:32 字数 1094 浏览 9 评论 0原文

我通过向已有的 JsonResult (从不同的方法返回)添加一些额外的信息来在 Controller 中构造我的 JsonResult。为了添加更多属性,我将最初的 JsonResult 转换为字典:

IDictionary<string, object> wrapper = (IDictionary<string, object>)new 
     System.Web.Routing.RouteValueDictionary(json.Data);

然后我只需通过编写 wrapper["..."] = "value" 来添加数据。

该方法返回一个新的 JsonResult,包装器为 .Data:,

new JsonResult() { wrapper, JsonRequestBehavior.AllowGet };

这就是麻烦开始的地方;虽然通信完美进行,并且成功函数被调用,但我在 JavaScript 中使用的结果数组没有我期望的干净结构:而不是像 val = ret.PropName1; 那样访问值必须访问一个简单的索引数组,其中又包含一个包含两对的字典: { "Value"="val, "Key"="PropName1" }; (所以类似于 o[0].Key 会给我属性名称)

我想知道是否有一种智能、快速的方法来重写控制器中的 JsonResult 创建,以便在视图中获得一个干净的字典。 我有几个想法,但它们并不是特别干净:我可以在服务器端放弃 JsonResult 重用,而只创建一个具有所有正确属性的匿名对象;或者,我可以在 Javascript 中创建一个翻译函数,将结果翻译成一个新的 Array()。我正在寻找更好的解决方案。

[稍后编辑] 该数组之所以如此,是因为字典被定义为 。如果它是 ,它将按照我最初期望的方式发送。但由于我实际上使用了该包中的对象,因此我将保持原样,并通过以下函数传递 json 响应。

I'm constructing my JsonResult in Controller by adding some extra information to an already existing JsonResult (returned from a different method). In order to add more properties, I converted the initial JsonResult into a Dictionary:

IDictionary<string, object> wrapper = (IDictionary<string, object>)new 
     System.Web.Routing.RouteValueDictionary(json.Data);

Then I just add data by writing wrapper["..."] = "value".

The method returns a new JsonResult, with wrapper as .Data:

new JsonResult() { wrapper, JsonRequestBehavior.AllowGet };

and that's where the troubles start; while communication happens perfectly, and the success function gets called, the resulting array which I use in JavaScript doesn't have the clean structure I expect: instead of accessing values as val = ret.PropName1; I end up having to access a simple indexed array, which contains in turn a dictionary with two pairs: { "Value"="val, "Key"="PropName1" }; (so something like o[0].Key would give me the property name)

I'd like to know if there's a smart, fast way to rewrite the JsonResult creation in the Controller, to get a nice clean dictionary in the View.
There are a couple of ideas I have, but they aren't particularly clean: I could throw away JsonResult reuse on the server side, and just make an anonymous object with all the right properties; or, I could make a translation function in Javascript which could translate the result into a new Array(). I'm looking for better solutions.

[Later Edit] The array comes the way it does because the dictionary was defined as <string, object>. If it were <string, string>, it would be sent the way I'd originally expect it. But since I actually use objects from that bag, I'll just leave it the way it is, and pass the json response through the below function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷︶言冷语的世界 2024-10-11 07:59:32

附录:在写上面的问题时,我想到“坏”数组和“好”数组之间的转换确实非常简单:

    function translateAjaxResult(ret) {
        var result = new Array();

        if (ret == null) return result;
        for(var i = 0; i < ret.length; i++)
            result[ret[i].Key] = ret[i].Value;
        return result;
    }

尽管如此,它仍然是问题的补丁,而不是问题的修复,所以我'我仍然喜欢更优雅的解决方案。

Addendum: while writing the above question, it occurred to me that the translation between 'bad' array and 'good' array is indeed very simple:

    function translateAjaxResult(ret) {
        var result = new Array();

        if (ret == null) return result;
        for(var i = 0; i < ret.length; i++)
            result[ret[i].Key] = ret[i].Value;
        return result;
    }

Nonetheless, it's still a patch to a problem and not a fix to a problem, so I'd still like a more elegant solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文