如何通过扁平化的JsonResult asp.net的MVC返回ExpandoObject? [英] How to flatten an ExpandoObject returned via JsonResult in asp.net mvc?

查看:139
本文介绍了如何通过扁平化的JsonResult asp.net的MVC返回ExpandoObject?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很喜欢 ExpandoObject 在编译运行时服务器端动态对象,但我有麻烦JSON序列化过程中出扁平化这件事情。首先,我实例化对象:

I really like the ExpandoObject while compiling a server-side dynamic object at runtime, but I am having trouble flattening this thing out during JSON serialization. First, I instantiate the object:

dynamic expando = new ExpandoObject();
var d = expando as IDictionary<string, object>;
expando.Add("SomeProp", SomeValueOrClass);

到目前为止好。在我的MVC控制器,我想再发下来作为一个JsonResult,所以我这样做:

So far so good. In my MVC controller, I want to then send this down as a JsonResult, so I do this:

return new JsonResult(expando);

此序列化JSON到下面的,由浏览器消耗:

This serializes the JSON into the below, to be consumed by the browser:

[{"Key":"SomeProp", "Value": SomeValueOrClass}]

,但我真正喜欢的是要看到这一点:

{SomeProp: SomeValueOrClass}

我知道我可以做到这一点,如果我使用动态而不是 ExpandoObject - JsonResult 能够序列化动态属性和值成一个单一的对象(不带钥匙或价值的业务),,但我之所以需要使用 ExpandoObject 是因为我不知道所有的对象我想要的属性,直到运行时,而据我所知,我不能动态一个属性添加到动态不使用 ExpandoObject

I know I can achieve this if I use dynamic instead of ExpandoObject -- JsonResult is able to serialize the dynamic properties and values into a single object (with no Key or Value business), but the reason I need to use ExpandoObject is because I don't know all of the properties I want on the object until runtime, and as far as I know, I cannot dynamically add a property to a dynamic without using an ExpandoObject.

我可能会通过钥匙,价值的经营在我的JavaScript进行筛选,但我希望先算出这个把它发送给客户端。感谢您的帮助!

I may have to sift through the "Key", "Value" business in my javascript, but I was hoping to figure this out prior to sending it to the client. Thanks for your help!

推荐答案

您也可以,让只适用于ExpandoObject特殊JSONConverter,然后在的JavaScriptSerializer实例注册。通过这种方式,直到找到另一种未得到正确序列化(U所希望的方式)的对象,你可以序列化的expando数组,对象的expando的组合和...,然后你再拍转换器,或添加其他类型这个。希望这有助于。

You could also, make a special JSONConverter that works only for ExpandoObject and then register it in an instance of JavaScriptSerializer. This way you could serialize arrays of expando,combinations of expando objects and ... until you find another kind of object that is not getting serialized correctly("the way u want"), then you make another Converter, or add another type to this one. Hope this helps.

using System.Web.Script.Serialization;    
public class ExpandoJSONConverter : JavaScriptConverter
{
    public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
    {
        throw new NotImplementedException();
    }
    public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
    {         
        var result = new Dictionary<string, object>();
        var dictionary = obj as IDictionary<string, object>;
        foreach (var item in dictionary)
            result.Add(item.Key, item.Value);
        return result;
    }
    public override IEnumerable<Type> SupportedTypes
    {
        get 
        { 
              return new ReadOnlyCollection<Type>(new Type[] { typeof(System.Dynamic.ExpandoObject) });
        }
    }
}

使用转换器

var serializer = new JavaScriptSerializer(); 
serializer.RegisterConverters(new JavaScriptConverter[] { new ExpandoJSONConverter()});
var json = serializer.Serialize(obj);

这篇关于如何通过扁平化的JsonResult asp.net的MVC返回ExpandoObject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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