ExpandoObject对象的JSON序列化
2014-04-19 13:37
253 查看
如果:
备注:其它的方法还有
一:
dynamic expando = new ExpandoObject();
d.SomeProp=SomeValueOrClass;
然后,我们在控制器中:d.SomeProp=SomeValueOrClass;
return new JsonResult(expando);
那么,我们的前台将会得到: [{"Key":"SomeProp", "Value": SomeValueOrClass}]
而实际上,我们知道,JSON 格式的内容,应该是这样的: {SomeProp: SomeValueOrClass}
于是乎,我们需要一个自定义的序列化器,它应该如下: public class ExpandoJSONConverter : JavaScriptConverter
{
public override IEnumerable<Type> SupportedTypes
{
get
{
return new ReadOnlyCollection<Type>(new Type[] { typeof(System.Dynamic.ExpandoObject) });
}
}
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) });
}
}
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 ContentResult GetSomeThing(string categores)
{
return ControllProctector.Do1(() =>
{
…
var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new JavaScriptConverter[] { new ExpandoJSONConverter() });
var json = serializer.Serialize(expando);
return new ContentResult
{
Content = json,
ContentType = "application/json"
};
});
}
我们的浏览器就能得到正确的 JSON 字符串了。{
return ControllProctector.Do1(() =>
{
…
var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new JavaScriptConverter[] { new ExpandoJSONConverter() });
var json = serializer.Serialize(expando);
return new ContentResult
{
Content = json,
ContentType = "application/json"
};
});
}
备注:其它的方法还有
一:
dynamic expando = new ExpandoObject();
expando.Blah = 42;
expando.Foo = "test";
...
var d = expando as IDictionary<string, object>;
d.Add("SomeProp", SomeValueOrClass);
// After you've added the properties you would like.
d = d.ToDictionary(x => x.Key, x => x.Value);
return new JsonResult(d);
二: JSON.NETexpando.Blah = 42;
expando.Foo = "test";
...
var d = expando as IDictionary<string, object>;
d.Add("SomeProp", SomeValueOrClass);
// After you've added the properties you would like.
d = d.ToDictionary(x => x.Key, x => x.Value);
return new JsonResult(d);
dynamic expando = new ExpandoObject();
expando.name = "John Smith";
expando.age = 30;
var json = JsonConvert.SerializeObject(expando);
三:Content-method:expando.name = "John Smith";
expando.age = 30;
var json = JsonConvert.SerializeObject(expando);
public ActionResult Data()
{
dynamic expando = new ExpandoObject();
expando.name = "John Smith";
expando.age = 30;
var json = JsonConvert.SerializeObject(expando);
return Content(json, "application/json");
}
参考:http://stackoverflow.com/questions/5156664/how-to-flatten-an-expandoobject-returned-via-jsonresult-in-asp-net-mvc
{
dynamic expando = new ExpandoObject();
expando.name = "John Smith";
expando.age = 30;
var json = JsonConvert.SerializeObject(expando);
return Content(json, "application/json");
}
相关文章推荐
- Hibernate 使用JUnit 测试出现java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V
- bug修复:Xcode 5运行工程常见错误
- Java - 对象(object) 详解
- Java - 面向对象(object oriented)编程 详解
- Objective-C 类基础(三)
- Objective-C 类基础(二)
- The Right Way to Swizzle in Objective-C
- iOS开发之Objective-C与JavaScript的交互
- Objective-C——消息、Category和Protocol
- org.hibernate.ObjectNotFoundException: No row with the given identifier exists
- Java NIO框架Netty教程(三) – Object对象传递
- 利用OBJECT_DEFINITION函数来代码存档
- net.sf.json.JSONObject optDouble 与getDouble opt** 与 get**
- qt object and thread/signal and slots
- ios扩展机制objc_setAssociatedObject,objc_getAssociatedObject
- Objective--C语言预处理命令之条件编译(#ifdef,#else,#endif,#if等)
- window.url.createobjecturl 兼容多个浏览器(IE,google,360,Safari,firefox)
- 罗大柚OpenGL ES教程系列LessonFour(Part X):rendering multiple objects with different texture
- Effective Objective C------ 1不要忘本
- object类详解