Json.Net 学习笔记(一)
2011-11-16 13:28
381 查看
使用Newtonsoft.Json这是一个开源的Json.Net库。
下载地址:http://json.codeplex.com/releases/view/50552。当前版本为 Release 8
从下载到的源代码中获取Newtonsoft.Json.Net20.dll,添加到自己的工程中。
using Newtonsoft.Json;
定义类:
public class Message
{
public string Address { get; set; }
[JsonProperty(TypeNameHandling = TypeNameHandling.All)]
public object Body { get; set; }
}
public class SearchDetails
{
public string Query { get; set; }
public string Language { get; set; }
}
测试:
Message message = new Message
{
Address = "http://google.com/",
Body = new SearchDetails { Query = "Json.Net", Language = "en-us" }
};
string jsonMsg = JsonConvert.SerializeObject(message, Formatting.Indented);//Indented表示以缩进形式显示结果
System.Diagnostics.Debug.Write(jsonMsg);
Message deserialized = JsonConvert.DeserializeObject<Message>(jsonMsg);
SearchDetails searchDetails = (SearchDetails)deserialized.Body;
Response.Write(searchDetails.Query + "," + searchDetails.Language + "<br/>");
Debug输出结果格式:
{
"Address": "http://google.com/",
"Body": {
"$type": "TestJsonSerialization.SearchDetails, TestJsonSerialization",
"Query": "Json.Net",
"Language": "en-us"
}
}
注:1.JsonProperty标记字段或属性,用来控制它作为一个Json对象的属性序列化。
2.TypeNameHandling 用来为Json序列化指定类型名。它有几个枚举值:
if ($ != jQuery) {
$ = jQuery.noConflict();
}
var isLogined = true;
var cb_blogId = 58700;
var cb_entryId = 2227789;
var cb_blogApp = "xiaojinhe2";
var cb_blogUserGuid = "70b8a083-0875-de11-be36-001cf0cd104b";
var cb_entryCreatedDate = '2011/10/28 15:44:00';
下载地址:http://json.codeplex.com/releases/view/50552。当前版本为 Release 8
从下载到的源代码中获取Newtonsoft.Json.Net20.dll,添加到自己的工程中。
using Newtonsoft.Json;
定义类:
public class Message
{
public string Address { get; set; }
[JsonProperty(TypeNameHandling = TypeNameHandling.All)]
public object Body { get; set; }
}
public class SearchDetails
{
public string Query { get; set; }
public string Language { get; set; }
}
测试:
Message message = new Message
{
Address = "http://google.com/",
Body = new SearchDetails { Query = "Json.Net", Language = "en-us" }
};
string jsonMsg = JsonConvert.SerializeObject(message, Formatting.Indented);//Indented表示以缩进形式显示结果
System.Diagnostics.Debug.Write(jsonMsg);
Message deserialized = JsonConvert.DeserializeObject<Message>(jsonMsg);
SearchDetails searchDetails = (SearchDetails)deserialized.Body;
Response.Write(searchDetails.Query + "," + searchDetails.Language + "<br/>");
Debug输出结果格式:
{
"Address": "http://google.com/",
"Body": {
"$type": "TestJsonSerialization.SearchDetails, TestJsonSerialization",
"Query": "Json.Net",
"Language": "en-us"
}
}
注:1.JsonProperty标记字段或属性,用来控制它作为一个Json对象的属性序列化。
2.TypeNameHandling 用来为Json序列化指定类型名。它有几个枚举值:
Member | Description |
---|---|
None | Do not include the .NET type name when serializing types. |
Objects | Include the .NET type name when serializing into a JSON object structure. |
Arrays | Include the .NET type name when serializing into a JSON array structure. |
Auto | Include the .NET type name when the type of the object being serialized is not the same as its declared type. |
All | Always include the .NET type name when serializing. |
$ = jQuery.noConflict();
}
var isLogined = true;
var cb_blogId = 58700;
var cb_entryId = 2227789;
var cb_blogApp = "xiaojinhe2";
var cb_blogUserGuid = "70b8a083-0875-de11-be36-001cf0cd104b";
var cb_entryCreatedDate = '2011/10/28 15:44:00';
相关文章推荐
- Newtonsoft.Json(Json.Net)学习笔记
- Newtonsoft.Json(Json.Net)学习笔记
- Json.Net学习笔记(十二) 协议解析
- Json.Net学习笔记(十二) 协议解析
- Newtonsoft.Json(Json.Net)学习笔记
- Newtonsoft.Json(Json.Net)学习笔记
- Newtonsoft.Json(Json.Net)学习笔记
- Newtonsoft.Json(Json.Net)学习笔记
- Newtonsoft.Json(Json.Net)学习笔记
- Newtonsoft.Json(Json.Net)学习笔记
- Newtonsoft.Json(Json.Net)学习笔记 转
- Json(Json.Net)学习笔记
- Newtonsoft.Json(Json.Net)学习笔记
- 黄聪:C#如何Json转字符串;字符串转Json;Newtonsoft.Json(Json.Net)学习笔记(转)
- Newtonsoft.Json(Json.Net)学习笔记
- Newtonsoft.Json(Json.Net)学习笔记
- Newtonsoft.Json(Json.Net)学习笔记
- Json.Net 学习笔记(二) Linq to Json
- Newtonsoft.Json(Json.Net)学习笔记
- Newtonsoft.Json(Json.Net)学习笔记