实体类
Serializing
1、序列化一个对像
代码
timeLine obj
=
new
timeLine();
obj.created_at = " 2010-7-15 " ;
obj.id = 2589 ;
obj.mms_img_pre = " http://www.abc.com " ;
obj.text = " message text " ;
obj.user = u;
this .Literal1.Text = JsonConvert.SerializeObject(obj, Formatting.Indented);
obj.created_at = " 2010-7-15 " ;
obj.id = 2589 ;
obj.mms_img_pre = " http://www.abc.com " ;
obj.text = " message text " ;
obj.user = u;
this .Literal1.Text = JsonConvert.SerializeObject(obj, Formatting.Indented);
结果
{
" created_at " : " 2010-7-15 " ,
" text " : " message text " ,
" id " : 2589 ,
" mms_img_pre " : " http://www.abc.com " ,
" user " : {
" name " : " jack " ,
" screen_name " : null ,
" location " : " shenzhen "
}
}
" created_at " : " 2010-7-15 " ,
" text " : " message text " ,
" id " : 2589 ,
" mms_img_pre " : " http://www.abc.com " ,
" user " : {
" name " : " jack " ,
" screen_name " : null ,
" location " : " shenzhen "
}
}
2、序列化一个对像的集合
代码
userInfo u
=
new
userInfo();
u.name = " jack " ;
u.location = " shenzhen " ;
timeLine obj = new timeLine();
obj.created_at = " 2010-7-15 " ;
obj.id = 2589 ;
obj.mms_img_pre = " http://www.abc.com " ;
obj.text = " message text " ;
obj.user = u;
timeLine obj2 = new timeLine();
obj2.created_at = " 2010-7-15 " ;
obj2.id = 2589 ;
obj2.mms_img_pre = " http://www.abc.com " ;
obj2.text = " message text " ;
List < timeLine > tls = new List < timeLine > ();
tls.Add(obj);
tls.Add(obj2);
this .Literal1.Text = JsonConvert.SerializeObject(tls, Formatting.Indented);
u.name = " jack " ;
u.location = " shenzhen " ;
timeLine obj = new timeLine();
obj.created_at = " 2010-7-15 " ;
obj.id = 2589 ;
obj.mms_img_pre = " http://www.abc.com " ;
obj.text = " message text " ;
obj.user = u;
timeLine obj2 = new timeLine();
obj2.created_at = " 2010-7-15 " ;
obj2.id = 2589 ;
obj2.mms_img_pre = " http://www.abc.com " ;
obj2.text = " message text " ;
List < timeLine > tls = new List < timeLine > ();
tls.Add(obj);
tls.Add(obj2);
this .Literal1.Text = JsonConvert.SerializeObject(tls, Formatting.Indented);
结果
代码
[
{
" created_at " : " 2010-7-15 " ,
" text " : " message text " ,
" id " : 2589 ,
" mms_img_pre " : " http://www.abc.com " ,
" user " : {
" name " : " jack " ,
" screen_name " : null ,
" location " : " shenzhen "
}
},
{
" created_at " : " 2010-7-15 " ,
" text " : " message text " ,
" id " : 2589 ,
" mms_img_pre " : " http://www.abc.com " ,
" user " : null
}
]
{
" created_at " : " 2010-7-15 " ,
" text " : " message text " ,
" id " : 2589 ,
" mms_img_pre " : " http://www.abc.com " ,
" user " : {
" name " : " jack " ,
" screen_name " : null ,
" location " : " shenzhen "
}
},
{
" created_at " : " 2010-7-15 " ,
" text " : " message text " ,
" id " : 2589 ,
" mms_img_pre " : " http://www.abc.com " ,
" user " : null
}
]
Deserializing
原始JSON字符串
代码
1、反序列化为一个对像
timeLine msg
=
JsonConvert.DeserializeObject
<
timeLine
>
(json_input);
2、反序列化为一个对像的集合
List
<
timeLine
>
msg
=
JsonConvert.DeserializeObject
<
List
<
timeLine
>>
(json_input);
关键字:JSON,JSON.NET,序列化,反序列化,解析
http://chy710.cnblogs.com
本文转自chy710博客园博客,原文链接:http://www.cnblogs.com/chy710/archive/2010/07/15/1778145.html
,如需转载请自行联系原作者