天天看點

ASP.NET:使用Newtonsoft.Json序列化和反序列化JSON對象的例子

下載下傳網址:http://json.codeplex.com/releases/view/97986

授權協定:The MIT License (MIT)

線上文檔:http://james.newtonking.com/projects/json/help

我下載下傳的是Json45r11.zip

Bin是發行版本,Source是其源碼,版本對應情況如下:

-Net40:

.NET latest (4.0 & 4.5)

-Net35:

.NET 3.5 SP1, Mono

-Net20:

.NET 2.0

-Silverlight:

Silverlight 4 & 5

-WindowsPhone:

Windows Phone 7 & 7.1

-Metro

Windows 8, WinRT 4.5

-Portable

Portable Class Library (.NET 4.0, WP7, SL4)

性能對比:

ASP.NET:使用Newtonsoft.Json序列化和反序列化JSON對象的例子

應用示例:先定義一個Person類

1 /// <summary>
2 /// 例子:定義一個人員類
3 /// </summary>
4 public class Person
5 {
6     public string Name { get; set; }
7     public int Age { get; set; }
8     public string[] Mobiles { get; set; }
9 }      

序列化和反序列化應用執行個體:

1 List<Person> person = new List<Person>(){
 2     new Person() {Name = "China Mobile", Age = 100, Mobiles = new string[] {"13800138000","10086"}},
 3     new Person() {Name = "China Telecom", Age = 100, Mobiles = new string[] {"10000","189"}},
 4     new Person() {Name = "China Unicom", Age = 100, Mobiles = new string[] {"10010"}}
 5 };
 6 
 7 // 序列化為JSON字串
 8 string _json = JsonConvert.SerializeObject(person);
 9 
10 // 反序列化為Person清單對象
11 person = JsonConvert.DeserializeObject<List<Person>>(_json);      

今天遇到一個特殊的JSON字串,我是這樣反序列化的,先定義一個Test測試類:

1     private class Test
 2     {
 3         public string alias { get; set; }
 4         public string key { get; set; }
 5         public string domId { get; set; }
 6         public Properties properties { get; set; }
 7         public List<JArray> children { get; set; }
 8         public List<KeyValue> childrens
 9         {
10             get
11             {
12                 List<KeyValue> test = new List<KeyValue>();
13                 if (children != null)
14                 {
15                     foreach (JArray child in children)
16                     {
17                         Test value = child.First.ToObject<Test>();
18                         String key = child.Last.ToString();
19                         KeyValue pair = new KeyValue(key, value);
20                         test.Add(pair);
21                     }
22                 }
23                 return test;
24             }
25         }
26     }
27 
28     private class Properties
29     {
30         public string dock { get; set; }
31         public int left { get; set; }
32     }      

上面實體類中使用了JArray,可以延伸出的還有JObject、JToken、JProperty等,下面是反序列化特殊格式JSON的具體代碼:

1 string _json = @"[{'alias':'lblFirst', 'key':'linb.UI.Label', 'domId':'linbUILabela', 'properties':{'dataType':'varchar', 'dataLength':'50', 'isEmpty':'yes', 'dock':'none', 'dockOrder':1, 'top':40, 'width':80, 'height':20, 'right':'auto', 'bottom':'auto', 'renderer':null, 'zIndex':1, 'tabindex':1, 'position':'absolute', 'caption':'我的标簽', 'hAlign':'right', 'vAlign':'top'}}, {'alias':'ctl_input1', 'key':'linb.UI.Input', 'domId':'linbUIInputa', 'properties':{'dirtyMark':true, 'dataType':'varchar', 'dataLength':'50', 'isEmpty':'yes', 'dock':'none', 'dockOrder':1, 'left':100, 'top':40, 'width':120, 'height':22, 'right':'auto', 'bottom':'auto', 'renderer':null, 'zIndex':1, 'tabindex':1, 'position':'absolute', 'labelPos':'left', 'labelCaption':'ctl_input1', 'labelHAlign':'right', 'type':'text'}}, {'alias':'ctl_input2', 'key':'linb.UI.Input', 'domId':'linbUIInputb', 'properties':{'dirtyMark':true, 'dataType':'varchar', 'dataLength':'50', 'isEmpty':'yes', 'dock':'none', 'dockOrder':1, 'left':280, 'top':40, 'width':240, 'height':22, 'right':'auto', 'bottom':'auto', 'renderer':null, 'zIndex':1, 'tabindex':1, 'position':'absolute', 'labelSize':120, 'labelPos':'left', 'labelCaption':'ctl_input2', 'labelHAlign':'right', 'type':'text'}}, {'alias':'ctl_tabs2', 'key':'linb.UI.Tabs', 'domId':'linbUITabsb', 'properties':{'dirtyMark':true, 'items':[{'id':'child_a', 'caption':'子表1'}, {'id':'child_b', 'caption':'子表2'}], 'dataType':'varchar', 'dataLength':'50', 'isEmpty':'yes', 'dock':'none', 'dockOrder':1, 'left':80, 'top':120, 'width':460, 'height':210, 'right':'auto', 'bottom':'auto', 'renderer':null, 'zIndex':1, 'tabindex':1, 'position':'absolute', 'HAlign':'left'}, 'children':[[{'alias':'ctl_input3', 'key':'linb.UI.Input', 'domId':'linbUIInputc', 'properties':{'dirtyMark':true, 'dataType':'varchar', 'dataLength':'50', 'isEmpty':'yes', 'dock':'none', 'dockOrder':1, 'left':40, 'top':10, 'width':120, 'height':22, 'right':'auto', 'bottom':'auto', 'renderer':null, 'zIndex':1, 'tabindex':1, 'position':'absolute', 'labelPos':'left', 'labelCaption':'ctl_input3', 'labelHAlign':'right', 'type':'text'}}, 'child_a'], [{'alias':'ctl_input4', 'key':'linb.UI.Input', 'domId':'linbUIInputd', 'properties':{'dirtyMark':true, 'dataType':'varchar', 'dataLength':'50', 'isEmpty':'yes', 'dock':'none', 'dockOrder':1, 'left':210, 'top':10, 'width':120, 'height':22, 'right':'auto', 'bottom':'auto', 'renderer':null, 'zIndex':1, 'tabindex':1, 'position':'absolute', 'labelPos':'left', 'labelCaption':'ctl_input4', 'labelHAlign':'right', 'type':'text'}}, 'child_a'], [{'alias':'ctl_input5', 'key':'linb.UI.Input', 'domId':'linbUIInpute', 'properties':{'dirtyMark':true, 'dataType':'varchar', 'dataLength':'50', 'isEmpty':'yes', 'dock':'none', 'dockOrder':1, 'left':70, 'top':40, 'width':120, 'height':22, 'right':'auto', 'bottom':'auto', 'renderer':null, 'zIndex':1, 'tabindex':1, 'position':'absolute', 'labelPos':'left', 'labelCaption':'ctl_input5', 'labelHAlign':'right', 'type':'text'}}, 'child_a'], [{'alias':'ctl_input6', 'key':'linb.UI.Input', 'domId':'linbUIInputf', 'properties':{'dirtyMark':true, 'dataType':'varchar', 'dataLength':'50', 'isEmpty':'yes', 'dock':'none', 'dockOrder':1, 'left':60, 'top':30, 'width':120, 'height':22, 'right':'auto', 'bottom':'auto', 'renderer':null, 'zIndex':1, 'tabindex':1, 'position':'absolute', 'labelPos':'left', 'labelCaption':'ctl_input6', 'labelHAlign':'right', 'type':'text'}}, 'child_b'], [{'alias':'ctl_input7', 'key':'linb.UI.Input', 'domId':'linbUIInputg', 'properties':{'dirtyMark':true, 'dataType':'varchar', 'dataLength':'50', 'isEmpty':'yes', 'dock':'none', 'dockOrder':1, 'left':220, 'top':20, 'width':120, 'height':22, 'right':'auto', 'bottom':'auto', 'renderer':null, 'zIndex':1, 'tabindex':1, 'position':'absolute', 'labelPos':'left', 'labelCaption':'ctl_input7', 'labelHAlign':'right', 'type':'text'}}, 'child_b'], [{'alias':'ctl_input8', 'key':'linb.UI.Input', 'domId':'linbUIInputh', 'properties':{'dirtyMark':true, 'dataType':'varchar', 'dataLength':'50', 'isEmpty':'yes', 'dock':'none', 'dockOrder':1, 'left':60, 'top':70, 'width':120, 'height':22, 'right':'auto', 'bottom':'auto', 'renderer':null, 'zIndex':1, 'tabindex':1, 'position':'absolute', 'labelPos':'left', 'labelCaption':'ctl_input8', 'labelHAlign':'right', 'type':'text'}}, 'child_b'], [{'alias':'ctl_input9', 'key':'linb.UI.Input', 'domId':'linbUIInputi', 'properties':{'dirtyMark':true, 'dataType':'varchar', 'dataLength':'50', 'isEmpty':'yes', 'dock':'none', 'dockOrder':1, 'left':230, 'top':60, 'width':120, 'height':22, 'right':'auto', 'bottom':'auto', 'renderer':null, 'zIndex':1, 'tabindex':1, 'position':'absolute', 'labelPos':'left', 'labelCaption':'ctl_input9', 'labelHAlign':'right', 'type':'text'}}, 'child_b']]}]";
 2     List<Test> tests = JsonConvert.DeserializeObject<List<Test>>(_json);
 3     foreach (var test in tests)
 4     {
 5         Console.Write(test.alias);
 6         Console.Write(test.key);
 7         Console.Write(test.domId);
 8         Console.Write(test.properties.dock);
 9         Console.Write(test.properties.left);
10 
11         foreach (KeyValue kv in test.childrens)
12         {
13             Console.Write(kv.Key);
14             Test c = (Test)kv.Value;
15         }
16     }      

上述由linb.net生成的JSON字串中,有個child_a、child_b,是沒有鍵值對應,且和并列的屬性類型不同,呵,是以隻好用JArray來解析了:)

其中用到的KeyValue,見此文:http://www.cnblogs.com/yipu/archive/2012/11/22/2782767.html

另外,用LINQ的方式來解析JSON也是可以的喔:

1     JArray arr = JArray.Parse(_json);
 2     var tableRows = from p in arr
 3         select new
 4         {
 5             alias = (string)p["alias"],
 6             key = (string)p["key"],
 7             domId = (string)p["domId"],
 8             properties = (JObject)p["properties"],
 9             children = (JArray)p["children"]
10         };
11     foreach (var i in tableRows)
12     {
13         Console.Write(i.alias);
14         Console.Write(i.key);
15         Console.Write(i.domId);
16         Console.Write(i.properties);
17         Console.Write(i.children);
18     }