首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web Service >

怎么利用json.net反序列化包含自定义类型的Arraylist

2013-01-08 
如何利用json.net反序列化包含自定义类型的Arraylist本帖最后由 maoxiajun 于 2012-06-11 15:46:25 编辑pu

如何利用json.net反序列化包含自定义类型的Arraylist
本帖最后由 maoxiajun 于 2012-06-11 15:46:25 编辑

public class a {
public int one {get;set;}
public ArrayList two{get;set;}
}
public class b {
public int three {get;set;}
public int four {get;set;}
}

比如有这样一个类,ArrayList中添加了自定义类b,

a tmp = new a();
b b1 = new b();
b b2 = new b();
a.two.Add(b1);a.two.Add(b2);

在用json.net转换为json后,在客户端反序列化之后,在对a类中的two进行类型转换时,即
(b[])a.two.ToArray(typeof(b))

时,运行时报错
无法将类型为“Newtonsoft.Json.Linq.JObject”的对象强制转换为类型“b”
求高手解释,tks。
[解决办法]
ArrayList不知道元素类型。反序列化时指定下。

热点排行