C#实现.NET通用JSON解析/构建类
本文将试图提供一个高度灵活的JSON通用类型(JsonObject),实现对JSON的解析及序列化。
假设JSON对象内容如下:
{
orders: {
date: '21:31:59',
name: 'Xfrog',
books: [{
name: 'C# 网络核心编程',
publish: '2010-3-24'
}, {
name: 'C#入门经典中文版',
publish: '2009-10-16'
}]
},
blog: 'http://www.cnblogs.com/xfrog'
}
orders: {
date: '21:31:59',
name: 'Xfrog',
books: [{
name: 'C# 网络核心编程',
publish: '2010-3-24'
}, {
name: 'C#入门经典中文版',
publish: '2009-10-16'
}]
},
blog: 'http://www.cnblogs.com/xfrog'
}
使用JsonObject来构建,可选择以下三种方式:
方式一:
//通过标准构造函数
JsonObject json = new JsonObject();
json["orders"] = new JsonProperty(new JsonObject());
json["blog"] = new JsonProperty("http://www.cnblogs.com/xfrog");
JsonObject config = json.Properties<JsonObject>("orders");
json["orders"]["date"] = new JsonProperty(DateTime.Now.ToLongTimeString());
json["orders"]["name"] = new JsonProperty("Xfrog");
json["orders"]["books"] = new JsonProperty();
JsonProperty book = json["orders"]["books"].Add(new JsonObject());
book["name"] = new JsonProperty("C# 网络核心编程");
book["publish"] = new JsonProperty("2010-3-24");
book = json["orders"]["books"].Add(new JsonObject());
book["name"] = new JsonProperty("C#入门经典中文版");
book["publish"] = new JsonProperty("2009-10-16");
JsonObject json = new JsonObject();
json["orders"] = new JsonProperty(new JsonObject());
json["blog"] = new JsonProperty("http://www.cnblogs.com/xfrog");
JsonObject config = json.Properties<JsonObject>("orders");
json["orders"]["date"] = new JsonProperty(DateTime.Now.ToLongTimeString());
json["orders"]["name"] = new JsonProperty("Xfrog");
json["orders"]["books"] = new JsonProperty();
JsonProperty book = json["orders"]["books"].Add(new JsonObject());
book["name"] = new JsonProperty("C# 网络核心编程");
book["publish"] = new JsonProperty("2010-3-24");
book = json["orders"]["books"].Add(new JsonObject());
book["name"] = new JsonProperty("C#入门经典中文版");
book["publish"] = new JsonProperty("2009-10-16");
方式二:
//通过回调函数简化对象的构建
JsonObject json2 = new JsonObject((a) =>
{
a["orders"] = new JsonProperty(new JsonObject((b) =>
{
b["date"] = new JsonProperty(DateTime.Now.ToLongTimeString());
b["name"] = new JsonProperty("Xfrog");
b["books"] = new JsonProperty();
b["books"].Add(new JsonObject((c) =>
{
c["name"] = new JsonProperty("C# 网络核心编程");
c["publish"] = new JsonProperty("2010-3-24");
}));
b["books"].Add(new JsonObject((c) =>
{
c["name"] = new JsonProperty("C#入门经典中文版");
c["publish"] = new JsonProperty("2009-10-16");
}));
}));
a["blog"] = new JsonProperty("http://www.cnblogs.com/xfrog");
});
JsonObject json2 = new JsonObject((a) =>
{
a["orders"] = new JsonProperty(new JsonObject((b) =>
{
b["date"] = new JsonProperty(DateTime.Now.ToLongTimeString());
b["name"] = new JsonProperty("Xfrog");
b["books"] = new JsonProperty();
b["books"].Add(new JsonObject((c) =>
{
c["name"] = new JsonProperty("C# 网络核心编程");
c["publish"] = new JsonProperty("2010-3-24");
}));
b["books"].Add(new JsonObject((c) =>
{
c["name"] = new JsonProperty("C#入门经典中文版");
c["publish"] = new JsonProperty("2009-10-16");
}));
}));
a["blog"] = new JsonProperty("http://www.cnblogs.com/xfrog");
});
方式三:
//通过字符串构建Json对象
JsonObject newObj = new JsonObject(jsonStr);
JsonObject newObj = new JsonObject(jsonStr);
获取Json对象属性值的方法,也有三种方式:
//通过泛型函数
Console.WriteLine(newObj["orders"].GetValue<JsonObject>()["books"].GetValue<List<JsonProperty>>()[1].GetValue<JsonObject>()["name"].Value);
//通过属性类型对应的属性
Console.WriteLine(newObj["orders"].Object["books"].Items[1].Object["name"].Value);
//如果属性为对象类型,可通过字符串索引简化
Console.WriteLine(newObj["orders"]["books"][1]["name"].Value);
Console.WriteLine(newObj["orders"].GetValue<JsonObject>()["books"].GetValue<List<JsonProperty>>()[1].GetValue<JsonObject>()["name"].Value);
//通过属性类型对应的属性
Console.WriteLine(newObj["orders"].Object["books"].Items[1].Object["name"].Value);
//如果属性为对象类型,可通过字符串索引简化
Console.WriteLine(newObj["orders"]["books"][1]["name"].Value);
直接使用ToString函数,将JsonObject转换为Json字符串:
String jsonStr = json.ToString();
注意:
我在重载ToString函数时,并没有将字符串转换为JavsScript字符串类型(即对需要转义的字符的处理),当然,要实现也是极其简单的。另外,对于带String参数的ToString,我也未做特殊处理,感兴趣的朋友可自行实现。
源代码下载:www.dbank.com/download.action?t=40&k=MTY5ODc3NTk=&pcode=LCwyODA2MzAsMjgwNjMw&rnd=4