您的位置:首页 > 编程语言 > ASP

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

2015-05-21 08:51 981 查看
下载网址: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)

性能对比:



应用示例:先定义一个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     }


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐