當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JSON serializing and deserializing using JSON.NET
生活随笔
收集整理的這篇文章主要介紹了
JSON serializing and deserializing using JSON.NET
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
實體類 ///<summary>
/// 用戶信息
///</summary>
publicclass userInfo
{
privatestring _name;
publicstring name
{
get { return _name; }
set { _name = value; }
}
privatestring _screen_name;
publicstring screen_name
{
get { return _screen_name; }
set { _screen_name = value; }
}
privatestring _location;
publicstring location
{
get { return _location; }
set { _location = value; }
}
}
///<summary>
/// 消息
///</summary>
publicclass timeLine
{
privatestring _created_at;
publicstring created_at
{
get { return _created_at; }
set { _created_at = value; }
}
privatestring _text;
publicstring text
{
get { return _text; }
set { _text = value; }
}
privateint _id;
publicint id
{
get { return _id; }
set { _id = value; }
}
privatestring _mms_img_pre;
publicstring mms_img_pre
{
get { return _mms_img_pre; }
set { _mms_img_pre = value; }
}
private userInfo _user;
public userInfo user
{
get { return _user; }
set { _user = value; }
}
}
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"
}
}
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": "Wed Jul 14 17:59:39 +0800 2010",
"text": "[56fe] http://zuosa.com/Status/78385734",
"id": 78385734,
"mms_img_pre": "http://zuosa.com/photo/mmspv/00/11/69/2011136682.jpg",
"mms_img": "http://zuosa.com/photo/mms/00/11/69/2011136682.jpg",
"user": {
"name": "Jack Cai",
"screen_name": "jc2009",
"location": "5e7f4e1c.6df15733"
}
},
{
"created_at": "Wed Jul 14 17:53:54 +0800 2010",
"text": "[56fe] http://zuosa.com/Status/78384980",
"id": 78384980,
"mms_img_pre": "http://zuosa.com/photo/mmspv/00/11/69/3061136673.jpg",
"mms_img": "http://zuosa.com/photo/mms/00/11/69/3061136673.jpg",
"user": {
"name": "Jack Cai",
"screen_name": "jc2009",
"location": "5e7f4e1c.6df15733"
}
},
{
"created_at": "Tue Jul 13 17:41:41 +0800 2010",
"text": "5fc382e56ca167096816606f7684573065b9ff0c523054ea91cc90fd662f57286d416d6a3002",
"id": 78228921,
"user": {
"name": "Jack Cai",
"screen_name": "jc2009",
"location": "5e7f4e1c.6df15733"
}
},
{
"created_at": "Wed Jul 07 10:50:07 +0800 2010",
"text": "505a5565? 597d4f3c560054953002",
"id": 77267585,
"user": {
"name": "Jack Cai",
"screen_name": "jc2009",
"location": "5e7f4e1c.6df15733"
}
}
]
/// 用戶信息
///</summary>
publicclass userInfo
{
privatestring _name;
publicstring name
{
get { return _name; }
set { _name = value; }
}
privatestring _screen_name;
publicstring screen_name
{
get { return _screen_name; }
set { _screen_name = value; }
}
privatestring _location;
publicstring location
{
get { return _location; }
set { _location = value; }
}
}
///<summary>
/// 消息
///</summary>
publicclass timeLine
{
privatestring _created_at;
publicstring created_at
{
get { return _created_at; }
set { _created_at = value; }
}
privatestring _text;
publicstring text
{
get { return _text; }
set { _text = value; }
}
privateint _id;
publicint id
{
get { return _id; }
set { _id = value; }
}
privatestring _mms_img_pre;
publicstring mms_img_pre
{
get { return _mms_img_pre; }
set { _mms_img_pre = value; }
}
private userInfo _user;
public userInfo user
{
get { return _user; }
set { _user = value; }
}
}
?
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);
結(jié)果
{"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);
結(jié)果
代碼 [{
"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字符串
代碼 [{
"created_at": "Wed Jul 14 17:59:39 +0800 2010",
"text": "[56fe] http://zuosa.com/Status/78385734",
"id": 78385734,
"mms_img_pre": "http://zuosa.com/photo/mmspv/00/11/69/2011136682.jpg",
"mms_img": "http://zuosa.com/photo/mms/00/11/69/2011136682.jpg",
"user": {
"name": "Jack Cai",
"screen_name": "jc2009",
"location": "5e7f4e1c.6df15733"
}
},
{
"created_at": "Wed Jul 14 17:53:54 +0800 2010",
"text": "[56fe] http://zuosa.com/Status/78384980",
"id": 78384980,
"mms_img_pre": "http://zuosa.com/photo/mmspv/00/11/69/3061136673.jpg",
"mms_img": "http://zuosa.com/photo/mms/00/11/69/3061136673.jpg",
"user": {
"name": "Jack Cai",
"screen_name": "jc2009",
"location": "5e7f4e1c.6df15733"
}
},
{
"created_at": "Tue Jul 13 17:41:41 +0800 2010",
"text": "5fc382e56ca167096816606f7684573065b9ff0c523054ea91cc90fd662f57286d416d6a3002",
"id": 78228921,
"user": {
"name": "Jack Cai",
"screen_name": "jc2009",
"location": "5e7f4e1c.6df15733"
}
},
{
"created_at": "Wed Jul 07 10:50:07 +0800 2010",
"text": "505a5565? 597d4f3c560054953002",
"id": 77267585,
"user": {
"name": "Jack Cai",
"screen_name": "jc2009",
"location": "5e7f4e1c.6df15733"
}
}
]
1、反序列化為一個對像
timeLine msg = JsonConvert.DeserializeObject<timeLine>(json_input);2、反序列化為一個對像的集合
List<timeLine> msg = JsonConvert.DeserializeObject<List<timeLine>>(json_input);?
關(guān)鍵字:JSON,JSON.NET,序列化,反序列化,解析 http://chy710.cnblogs.com?
轉(zhuǎn)載于:https://www.cnblogs.com/chy710/archive/2010/07/15/1778145.html
總結(jié)
以上是生活随笔為你收集整理的JSON serializing and deserializing using JSON.NET的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对DataTable的一些解释
- 下一篇: C#winform抓取百度,Google