JSON serializing and deserializing using JSON.NET

实体类

 

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);
复制代码

结果

复制代码
{
" 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);
复制代码

结果

复制代码
代码
[
{
" 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 ,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值