C# 无法将dynamicJsonObject转换为字典<;字符串,对象>;
我正在使用DynamicJSON反序列化程序来反序列化我的json,它工作得很好C# 无法将dynamicJsonObject转换为字典<;字符串,对象>;,c#,json,C#,Json,我正在使用DynamicJSON反序列化程序来反序列化我的json,它工作得很好 public class DynamicJsonObject : DynamicObject { private IDictionary<string, object> Dictionary { get; set; } public DynamicJsonObject(IDictionary<string, object> dictionary) {
public class DynamicJsonObject : DynamicObject
{
private IDictionary<string, object> Dictionary { get; set; }
public DynamicJsonObject(IDictionary<string, object> dictionary)
{
this.Dictionary = dictionary;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = this.Dictionary[binder.Name];
if (result is IDictionary<string, object>)
{
result = new DynamicJsonObject(result as IDictionary<string, object>);
}
else if (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>)));
}
else if (result is ArrayList)
{
result = new List<object>((result as ArrayList).ToArray());
}
return this.Dictionary.ContainsKey(binder.Name);
}
}
public class DynamicJsonConverter : JavaScriptConverter
{
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
if (dictionary == null)
throw new ArgumentNullException("dictionary");
if (type == typeof(object))
{
return new DynamicJsonObject(dictionary);
}
return null;
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
public override IEnumerable<Type> SupportedTypes
{
get { return new ReadOnlyCollection<Type>(new List<Type>(new Type[] { typeof(object) })); }
}
}
公共类DynamicJsonObject:DynamicObject
{
专用IDictionary字典{get;set;}
公共动态对象(IDictionary dictionary)
{
这个。字典=字典;
}
公共重写bool TryGetMember(GetMemberBinder绑定器,输出对象结果)
{
结果=this.Dictionary[binder.Name];
如果(结果是IDictionary)
{
结果=新的DynamicJsonObject(结果为IDictionary);
}
else if(结果为ArrayList&&(结果为ArrayList)为IDictionary)
{
结果=新列表((结果为ArrayList.ToArray()。选择(x=>new DynamicJsonObject(x为IDictionary)));
}
else if(结果为ArrayList)
{
结果=新列表((结果为ArrayList.ToArray());
}
返回this.Dictionary.ContainsKey(binder.Name);
}
}
公共类DynamicJsonConverter:JavaScriptConverter
{
公共重写对象反序列化(IDictionary dictionary、类型、JavaScriptSerializer序列化程序)
{
if(dictionary==null)
抛出新的ArgumentNullException(“字典”);
if(type==typeof(object))
{
返回新的DynamicJsonObject(字典);
}
返回null;
}
公共重写IDictionary序列化(对象obj、JavaScriptSerializer序列化程序)
{
抛出新的NotImplementedException();
}
公共覆盖IEnumerable SupportedTypes
{
获取{return new ReadOnlyCollection(new List(new Type[]{typeof(object)}));}
}
}
为什么我不能将DynamicJsonObject转换成字典,这样我就可以用它做一些事情
dynamic obj = serializer.Deserialize(jsonResponse, typeof(object));
Dictionary<string, object> test = new Dictionary<string, object>(dynamicObject.Dictionary); //cannot do this
dynamic obj=serializer.Deserialize(jsonResponse,typeof(object));
字典测试=新字典(dynamicObject.Dictionary)//不能这样做
我的问题通过公开词典很容易解决 评论员:这是一个答案。它解释了他们是如何解决这个问题的。