If:
Dynamic expando = new ExpandoObject ();
D.someprop=somevalueorclass;
And then, we're in the controller:
return new Jsonresult (expando);
Well, our front desk will get:
[{' Key ': ' Someprop ', ' Value ': Somevalueorclass}]
And in fact, we know that JSON-formatted content should be like this:
{Someprop:somevalueorclass}
So, we need a custom serializer, which should be as follows:
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;
}
Now, our controller should write like this:
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 '
};
}
Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/