Asp.net3.5 实现Json与.net实体类相互转换
Asp.net3.5中,System.Web.Script.Serialization包括一个JavaScriptSerializer类,该可以实现Json与.net实体类之间相互转换。
下面是一个例子
1 JavaScriptSerializer jserializer = new JavaScriptSerializer();
2
3 User user = jserializer.Deserialize<User>("{\"UserName\":\"user\",\"TrueName\":\"wange\",\"Address\":{\"City\":\"beijing\",\"Country\":\"China\"},\"AddressList\":[{\"City\":\"ShangHai\",\"Country\":\"China\"},{\"City\":\"ShiJiaZhuang\",\"Country\":\"China\"}]}");//将Json字符串转换为实体类
4
5 StringBuilder stringBuilder=new StringBuilder();
6 jserializer.Serialize(user, stringBuilder);//将实体转换为