JSON.NET與LINQ序列化示例教
這篇文章通過多個示例演示JSON.NET
與LINQ
的完美結合,讓.NET操作JSON
變得非常的簡單,最後我們還會演示如何通過JSON.NET
轉換BSON
資料、如何實現JSON
格式與XML
格式的互相轉換,讓您熟練在.NET
中序列化物件。
1、手動建立JSON
物件和陣列
JSON
格式主要包括物件
和陣列
兩種形式,在JSON.NET
中分別用JArray
和JObject
表示,屬性用JProperty
表示,屬性值用JValue
表示,這些物件都直接或間接繼承JToken
抽象類,而且都有一個ToString
方法,該方法用於直接輸出元素的JSON
字串表示形式,我們可以通過JArray
和JObject
手動建立JSON
JArray array = new JArray();
array.Add("Manual text");
array.Add(new DateTime(2000, 5, 23));
JObject o = new JObject();
o["MyArray"] = array;
string json = o.ToString();
{
"MyArray": [
"Manual text",
"2000-05-23T00:00:00"
]
}
2、用集合的方式初始化JArray
陣列和JObject
物件
JObject o = new JObject { { "Cpu" , "Intel" }, { "Memory", 32 }, { "Drives", new JArray { "DVD", "SSD" } } };
Console.WriteLine(o.ToString());
{
"Cpu": "Intel",
"Memory": 32,
"Drives": [
"DVD",
"SSD"
]
}
3、使用動態型別dynamic
型別建立JSON
陣列和物件
C#中的動態型別dynamic
允許開發者動態構造物件,這些動態構造的物件在執行時被解析執行,JSON.NET
支援動態型別,無需定義類,即可構造JSON
,這一些就像在JavaScript
dynamic product = new JObject();
product.ProductName = "Elbow Grease";
product.Enabled = true;
product.Price = 4.90m;
product.StockCount = 9000;
product.StockValue = 44100;
product.Tags = new JArray("Real", "OnSale");
Console.WriteLine(product.ToString());
{
"ProductName": "Elbow Grease",
"Enabled": true,
"Price": 4.90,
"StockCount": 9000,
"StockValue": 44100,
"Tags": [
"Real",
"OnSale"
]
}
4、使用JTokenWriter
建立物件和陣列
JTokenWriter writer = new JTokenWriter();
writer.WriteStartObject();
writer.WritePropertyName("name1");
writer.WriteValue("value1");
writer.WritePropertyName("name2");
writer.WriteStartArray();
writer.WriteValue(1);
writer.WriteValue(2);
writer.WriteEndArray();
writer.WriteEndObject();
JObject o = (JObject)writer.Token;
Console.WriteLine(o.ToString());
{
"name1": "value1",
"name2": [
1,
2
]
}
5、使用物件建立JSON物件和陣列
JToken
、JObject
和JArray
都提供一個名為FromObject
的方法,允許通過一個物件來建立JSON
物件。
Computer computer = new Computer
{
Cpu = "Intel",
Memory = 32,
Drives = new List<string> { "DVD", "SSD" }
};
JObject o = (JObject)JToken.FromObject(computer);
Console.WriteLine(o.ToString());
{
"Cpu": "Intel",
"Memory": 32,
"Drives": [
"DVD",
"SSD"
]
}
JArray a = (JArray)JToken.FromObject(computer.Drives);
Console.WriteLine(a.ToString());
[
"DVD",
"SSD"
]
6、使用字串建立JSON
物件和陣列
string json = @"['Small', 'Medium','Large']";
JArray a = JArray.Parse(json);
Console.WriteLine(a.ToString());
[
"Small",
"Medium",
"Large"
]
string json = @"{
CPU: 'Intel',
Drives: [
'DVD read/writer',
'500 gigabyte hard drive'
]
}";
JObject o = JObject.Parse(json);
Console.WriteLine(o.ToString());
{
"CPU": "Intel",
"Drives": [
"DVD read/writer",
"500 gigabyte hard drive"
]
}
7、使用JToken
轉換JSON
字串
在示例1中,我們已經說明JToken
型別,它是JSON.NET
的核心,幾乎所有的JSON
元素都繼承於JToken
抽象類,雖然JToken
是抽象類,但它也提供一些很有用的基礎方法,可以轉換一段字串並判斷它應有的JSON
型別。
JToken t1 = JToken.Parse("{}");
Console.WriteLine(t1.Type);
// Object
JToken t2 = JToken.Parse("[]");
Console.WriteLine(t2.Type);
// Array
JToken t3 = JToken.Parse("null");
Console.WriteLine(t3.Type);
// Null
JToken t4 = JToken.Parse(@"'A string!'");
Console.WriteLine(t4.Type);
// String
8、動態修改JSON
物件和陣列
string json = @"{
'channel': {
'title': 'Star Wars',
'link': 'www.xcode.me',
'description': 'Star Wars blog.',
'obsolete': 'Obsolete value',
'item': []
}
}";
JObject rss = JObject.Parse(json);
JObject channel = (JObject)rss["channel"];
channel["title"] = ((string)channel["title"]).ToUpper();
channel["description"] = ((string)channel["description"]).ToUpper();
channel.Property("obsolete").Remove();
channel.Property("description").AddAfterSelf(new JProperty("new", "New value"));
JArray item = (JArray)channel["item"];
item.Add("Item 1");
item.Add("Item 2");
Console.WriteLine(rss.ToString());
{
"channel": {
"title": "STAR WARS",
"link": "www.xcode.com",
"description": "STAR WARS BLOG.",
"new": "New value",
"item": [
"Item 1",
"Item 2"
]
}
}
9、合併兩個JSON
物件
JObject o1 = JObject.Parse(@"{
'FirstName': 'John',
'LastName': 'Smith',
'Enabled': false,
'Roles': [ 'User' ]
}");
JObject o2 = JObject.Parse(@"{
'Enabled': true,
'Roles': [ 'User', 'Admin' ]
}");
o1.Merge(o2, new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Union });
string json = o1.ToString();
{
"FirstName": "John",
"LastName": "Smith",
"Enabled": true,
"Roles": [
"User",
"Admin"
]
}
這裡要特別說明的是JsonMergeSettings
,該物件用於設定合併時的行為,MergeArrayHandling
是列舉,可設定為Concat
(連線陣列)、Union
(跳過存在的元素,新增不存在的元素)、Replace
(替換所有陣列項)和Merge
(根據陣列索引將陣列項合併在一起)。
10、使用動態型別dynamic
查詢JSON
物件
string json = @"[
{
'Title': 'Json.NET is awesome!',
'Author': {
'Name': 'James Newton-King',
'Twitter': '@JamesNK',
'Picture': '/jamesnk.png'
},
'Date': '2013-01-23T19:30:00'
}
]";
dynamic blogPosts = JArray.Parse(json);
dynamic blogPost = blogPosts[0];
Console.WriteLine(blogPost.Title);
Json.NET is awesome!
11、使用ToObject
將JSON
物件或陣列轉換成C#
物件
string json = @"{
'd': [
{
'Name': 'John Smith'
},
{
'Name': 'Mike Smith'
}
]
}";
JObject o = JObject.Parse(json);
JArray a = (JArray)o["d"];
IList<Person> person = a.ToObject<IList<Person>>();
Console.WriteLine(person[1].Name);
Mike Smith
12、通過屬性遍歷JObject
物件
JObject o = new JObject { { "name1", "value1" }, { "name2", "value2" } };
foreach (JProperty property in o.Properties())
{
Console.WriteLine(property.Name + " - " + property.Value);
}
name1 - value1
name2 - value2
foreach (KeyValuePair<string, JToken> property in o)
{
Console.WriteLine(property.Key + " - " + property.Value);
}
name1 - value1
name2 - value2
13、使用ToString
方法進行JSON
序列化
在JSON.NET
中ToString
方法會輸出針對當前JSON
物件的字串表示形式,通過這個特點我們可以序列化物件,預設情況下會輸出格式化後的JSON
字串,ToString
方法提供一些過載,允許開發靈活配置序列化行為。
JObject o = JObject.Parse(@"{'string1':'value','integer2':99,'datetime3':'2000-05-23T00:00:00'}");
Console.WriteLine(o.ToString());
{
"string1": "value",
"integer2": 99,
"datetime3": "2000-05-23T00:00:00"
}
ToString
方法也提供一個是否格式化過載方法,下面的示例演示輸出壓縮後的JSON
字串。
Console.WriteLine(o.ToString(Formatting.None));
{"string1":"value","integer2":99,"datetime3":"2000-05-23T00:00:00"}
下面的程式碼演示通過ToString
方法提供的過載將日期格式化為JavaScript
中的日期物件,這樣的JSON
在JS
中就可以直接使用日期。
Console.WriteLine(o.ToString(Formatting.None, new JavaScriptDateTimeConverter()));
{"string1":"value","integer2":99,"datetime3":new Date(959032800000)}
14、JSON.NET
支援BSON
格式的序列化和反序列化
BSON
是一種類似於JSON
格式,是一種二進位制形式儲存格式,簡稱BinaryJSON
,它和JSON
一樣,支援內嵌的物件和陣列,BSON
可以做為網路資料交換的一種儲存形式,它的優點是靈活性高,但它的缺點是空間利用率不是很理想,JSON.NET
也支援BSON
的序列化和反序列化。
byte[] data = Convert.FromBase64String("KQAAAAJuYW1lMQAHAAAAdmFsdWUxAAJuYW1lMgAHAAAAdmFsdWUyAAA=");
MemoryStream ms = new MemoryStream(data);
JObject o;
using (BsonReader reader = new BsonReader(ms))
{
o = (JObject)JToken.ReadFrom(reader);
}
string value = (string)o["name1"];
Console.WriteLine(value);
value1
JObject o = new JObject { { "name1", "value1" }, { "name2", "value2" } };
MemoryStream ms = new MemoryStream();
using (BsonWriter writer = new BsonWriter(ms))
{
o.WriteTo(writer);
}
string data = Convert.ToBase64String(ms.ToArray());
Console.WriteLine(data);
KQAAAAJuYW1lMQAHAAAAdmFsdWUxAAJuYW1lMgAHAAAAdmFsdWUyAAA=
15、JSON.NET
也支援JSON
與XML
資料格式之間的互轉
string json = @"{
'@Id': 1,
'Email': '[email protected]',
'Active': true,
'CreatedDate': '2013-01-20T00:00:00Z',
'Roles': [
'User',
'Admin'
],
'Team': {
'@Id': 2,
'Name': 'Software Developers',
'Description': 'Creators of fine software products and services.'
}
}";
XNode node = JsonConvert.DeserializeXNode(json, "Root");
Console.WriteLine(node.ToString());
<Root Id="1">
<Email>[email protected]</Email>
<Active>true</Active>
<CreatedDate>2013-01-20T00:00:00Z</CreatedDate>
<Roles>User</Roles>
<Roles>Admin</Roles>
<Team Id="2">
<Name>Software Developers</Name>
<Description>Creators of fine software products and services.</Description>
</Team>
</Root>
string xml = @"<?xml version='1.0' standalone='no'?>
<root>
<person id='1'>
<name>Alan</name>
<url>www.xocde.me</url>
</person>
<person id='2'>
<name>Louis</name>
<url>www.xocde.me</url>
</person>
</root>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string json = JsonConvert.SerializeXmlNode(doc);
Console.WriteLine(json);
{
"?xml": {
"@version": "1.0",
"@standalone": "no"
},
"root": {
"person": [
{
"@id": "1",
"name": "Alan",
"url": "www.xocde.me"
},
{
"@id": "2",
"name": "Louis",
"url": "www.xocde.me"
}
]
}
}
當然,JSON.NET
框架很強大,功能也很多,總結都是JSON.NET
與LINQ
結合的常用示例,希望對您有所幫助。