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

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

2012-11-22 16:04 891 查看
下载网址: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类

/// <summary>
/// 例子:定义一个人员类
/// </summary>
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string[] Mobiles { get; set; }
}


序列化和反序列化应用实例:

List<Person> person = new List<Person>(){
new Person() {Name = "China Mobile", Age = 100, Mobiles = new string[] {"13800138000","10086"}},
new Person() {Name = "China Telecom", Age = 100, Mobiles = new string[] {"10000","189"}},
new Person() {Name = "China Unicom", Age = 100, Mobiles = new string[] {"10010"}}
};

// 序列化为JSON字串
string _json = JsonConvert.SerializeObject(person);

// 反序列化为Person列表对象
person = JsonConvert.DeserializeObject<List<Person>>(_json);


今天遇到一个特殊的JSON字串,我是这样反序列化的,先定义一个Test测试类:

private class Test
{
public string alias { get; set; }
public string key { get; set; }
public string domId { get; set; }
public Properties properties { get; set; }
public List<JArray> children { get; set; }
public List<KeyValue> childrens
{
get
{
List<KeyValue> test = new List<KeyValue>();
if (children != null)
{
foreach (JArray child in children)
{
Test value = child.First.ToObject<Test>();
String key = child.Last.ToString();
KeyValue pair = new KeyValue(key, value);
test.Add(pair);
}
}
return test;
}
}
}

private class Properties
{
public string dock { get; set; }
public int left { get; set; }
}


上面实体类中使用了JArray,可以延伸出的还有JObject、JToken、JProperty等,下面是反序列化特殊格式JSON的具体代码:

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']]}]";
List<Test> tests = JsonConvert.DeserializeObject<List<Test>>(_json);
foreach (var test in tests)
{
Console.Write(test.alias);
Console.Write(test.key);
Console.Write(test.domId);
Console.Write(test.properties.dock);
Console.Write(test.properties.left);

foreach (KeyValue kv in test.childrens)
{
Console.Write(kv.Key);
Test c = (Test)kv.Value;
}
}


上述由linb.net生成的JSON字串中,有个child_a、child_b,是没有键值对应,且和并列的属性类型不同,呵,所以只好用JArray来解析了:)

其中用到的KeyValue,见此文:/article/4992240.html

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

JArray arr = JArray.Parse(_json);
var tableRows = from p in arr
select new
{
alias = (string)p["alias"],
key = (string)p["key"],
domId = (string)p["domId"],
properties = (JObject)p["properties"],
children = (JArray)p["children"]
};
foreach (var i in tableRows)
{
Console.Write(i.alias);
Console.Write(i.key);
Console.Write(i.domId);
Console.Write(i.properties);
Console.Write(i.children);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: