任意 JSON 转成 C# 动态类,无需事先声明一个C#类型,实现json字符串转成dynamic 类
示例:
class Program
{
staticvoid Main(string[] args)
{
string json ="{name:'hooyes',pwd:'hooyespwd',books:{a:'红楼梦',b:'水浒传',c:{arr:['宝玉','林黛玉']}},arr:['good','very good']}";
dynamic dy = ConvertJson(json);
Console.WriteLine(dy.name);
Console.WriteLine(dy.books.a);
Console.WriteLine(dy.arr[1]);
foreach (var s in dy.books.c.arr)
{
Console.WriteLine(s);
}
Console.Read();
}
static dynamic ConvertJson(string json)
{
JavaScriptSerializer jss =new JavaScriptSerializer();
jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() });
dynamic dy = jss.Deserialize(json, typeof(object)) as dynamic;
return dy;
}
}
{
staticvoid Main(string[] args)
{
string json ="{name:'hooyes',pwd:'hooyespwd',books:{a:'红楼梦',b:'水浒传',c:{arr:['宝玉','林黛玉']}},arr:['good','very good']}";
dynamic dy = ConvertJson(json);
Console.WriteLine(dy.name);
Console.WriteLine(dy.books.a);
Console.WriteLine(dy.arr[1]);
foreach (var s in dy.books.c.arr)
{
Console.WriteLine(s);
}
Console.Read();
}
static dynamic ConvertJson(string json)
{
JavaScriptSerializer jss =new JavaScriptSerializer();
jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() });
dynamic dy = jss.Deserialize(json, typeof(object)) as dynamic;
return dy;
}
}
用到两个自定义的类:DynamicJsonConverter,DynamicJsonObject
代码
publicclass DynamicJsonConverter : JavaScriptConverter
{
publicoverrideobject Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
if (dictionary ==null)
thrownew ArgumentNullException("dictionary");
if (type ==typeof(object))
{
returnnew DynamicJsonObject(dictionary);
}
returnnull;
}
publicoverride IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
thrownew NotImplementedException();
}
publicoverride IEnumerable<Type> SupportedTypes
{
get { returnnew ReadOnlyCollection<Type>(new List<Type>(new Type[] { typeof(object) })); }
}
}
{
publicoverrideobject Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
if (dictionary ==null)
thrownew ArgumentNullException("dictionary");
if (type ==typeof(object))
{
returnnew DynamicJsonObject(dictionary);
}
returnnull;
}
publicoverride IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
thrownew NotImplementedException();
}
publicoverride IEnumerable<Type> SupportedTypes
{
get { returnnew ReadOnlyCollection<Type>(new List<Type>(new Type[] { typeof(object) })); }
}
}
代码
publicclass DynamicJsonObject : DynamicObject
{
private IDictionary<string, object> Dictionary { get; set; }
public DynamicJsonObject(IDictionary<string, object> dictionary)
{
this.Dictionary = dictionary;
}
publicoverridebool TryGetMember(GetMemberBinder binder, outobject result)
{
result =this.Dictionary[binder.Name];
if (result is IDictionary<string, object>)
{
result =new DynamicJsonObject(result as IDictionary<string, object>);
}
elseif (result is ArrayList && (result as ArrayList) is IDictionary<string, object>)
{
result =new List<DynamicJsonObject>((result as ArrayList).ToArray().Select(x =>new DynamicJsonObject(x as IDictionary<string, object>)));
}
elseif (result is ArrayList)
{
result =new List<object>((result as ArrayList).ToArray());
}
returnthis.Dictionary.ContainsKey(binder.Name);
}
}
{
private IDictionary<string, object> Dictionary { get; set; }
public DynamicJsonObject(IDictionary<string, object> dictionary)
{
this.Dictionary = dictionary;
}
publicoverridebool TryGetMember(GetMemberBinder binder, outobject result)
{
result =this.Dictionary[binder.Name];
if (result is IDictionary<string, object>)
{
result =new DynamicJsonObject(result as IDictionary<string, object>);
}
elseif (result is ArrayList && (result as ArrayList) is IDictionary<string, object>)
{
result =new List<DynamicJsonObject>((result as ArrayList).ToArray().Select(x =>new DynamicJsonObject(x as IDictionary<string, object>)));
}
elseif (result is ArrayList)
{
result =new List<object>((result as ArrayList).ToArray());
}
returnthis.Dictionary.ContainsKey(binder.Name);
}
}