淺析ASP.NET萬(wàn)能JSON解析器
概念介紹
還是先簡(jiǎn)單說(shuō)說(shuō)Json的一些例子吧。注意,以下概念是我自己定義的,可以參考.net里面的TYPE的模型設(shè)計(jì)
如果有爭(zhēng)議,歡迎提出來(lái)探討!
1.最簡(jiǎn)單:
{"total":0}
total就是值,值是數(shù)值,等于0
2. 復(fù)雜點(diǎn)
{"total":0,"data":{"377149574" : 1}}
total是值,data是對(duì)象,這個(gè)對(duì)象包含了"377149574"這個(gè)值,等于1
3. 最復(fù)雜
{"total":0,"data":{"377149574":[{"cid":"377149574"}]}}
total是值,data是對(duì)象,377149574是數(shù)組,這個(gè)數(shù)組包含了一些列的對(duì)象,例如{"cid":"377149574"}這個(gè)對(duì)象。
有了以上的概念,就可以設(shè)計(jì)出通用的json模型了。
萬(wàn)能JSON源碼:
using System;
using System.Collections.Generic;
using System.Text;
namespace Pixysoft.Json
{
public class CommonJsonModelAnalyzer
{
protected string _GetKey(string rawjson)
{
if (string.IsNullOrEmpty(rawjson))
return rawjson;
rawjson = rawjson.Trim();
string[] jsons = rawjson.Split(new char[] { ':' });
if (jsons.Length < 2)
return rawjson;
return jsons[0].Replace("\"", "").Trim();
}
protected string _GetValue(string rawjson)
{
if (string.IsNullOrEmpty(rawjson))
return rawjson;
rawjson = rawjson.Trim();
string[] jsons = rawjson.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
if (jsons.Length < 2)
return rawjson;
StringBuilder builder = new StringBuilder();
for (int i = 1; i < jsons.Length; i++)
{
builder.Append(jsons[i]);
builder.Append(":");
}
if (builder.Length > 0)
builder.Remove(builder.Length - 1, 1);
string value = builder.ToString();
if (value.StartsWith("\""))
value = value.Substring(1);
if (value.EndsWith("\""))
value = value.Substring(0, value.Length - 1);
return value;
}
protected List<string> _GetCollection(string rawjson)
{
//[{},{}]
List<string> list = new List<string>();
if (string.IsNullOrEmpty(rawjson))
return list;
rawjson = rawjson.Trim();
StringBuilder builder = new StringBuilder();
int nestlevel = -1;
int mnestlevel = -1;
for (int i = 0; i < rawjson.Length; i++)
{
if (i == 0)
continue;
else if (i == rawjson.Length - 1)
continue;
char jsonchar = rawjson[i];
if (jsonchar == '{')
{
nestlevel++;
}
if (jsonchar == '}')
{
nestlevel--;
}
if (jsonchar == '[')
{
mnestlevel++;
}
if (jsonchar == ']')
{
mnestlevel--;
}
if (jsonchar == ',' && nestlevel == -1 && mnestlevel == -1)
{
list.Add(builder.ToString());
builder = new StringBuilder();
}
else
{
builder.Append(jsonchar);
}
}
if (builder.Length > 0)
list.Add(builder.ToString());
return list;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Pixysoft.Json
{
public class CommonJsonModel : CommonJsonModelAnalyzer
{
private string rawjson;
private bool isValue = false;
private bool isModel = false;
private bool isCollection = false;
internal CommonJsonModel(string rawjson)
{
this.rawjson = rawjson;
if (string.IsNullOrEmpty(rawjson))
throw new Exception("missing rawjson");
rawjson = rawjson.Trim();
if (rawjson.StartsWith("{"))
{
isModel = true;
}
else if (rawjson.StartsWith("["))
{
isCollection = true;
}
else
{
isValue = true;
}
}
public string Rawjson
{
get { return rawjson; }
}
public bool IsValue()
{
return isValue;
}
public bool IsValue(string key)
{
if (!isModel)
return false;
if (string.IsNullOrEmpty(key))
return false;
foreach (string subjson in base._GetCollection(this.rawjson))
{
CommonJsonModel model = new CommonJsonModel(subjson);
if (!model.IsValue())
continue;
if (model.Key == key)
{
CommonJsonModel submodel = new CommonJsonModel(model.Value);
return submodel.IsValue();
}
}
return false;
}
public bool IsModel()
{
return isModel;
}
public bool IsModel(string key)
{
if (!isModel)
return false;
if (string.IsNullOrEmpty(key))
return false;
foreach (string subjson in base._GetCollection(this.rawjson))
{
CommonJsonModel model = new CommonJsonModel(subjson);
if (!model.IsValue())
continue;
if (model.Key == key)
{
CommonJsonModel submodel = new CommonJsonModel(model.Value);
return submodel.IsModel();
}
}
return false;
}
public bool IsCollection()
{
return isCollection;
}
public bool IsCollection(string key)
{
if (!isModel)
return false;
if (string.IsNullOrEmpty(key))
return false;
foreach (string subjson in base._GetCollection(this.rawjson))
{
CommonJsonModel model = new CommonJsonModel(subjson);
if (!model.IsValue())
continue;
if (model.Key == key)
{
CommonJsonModel submodel = new CommonJsonModel(model.Value);
return submodel.IsCollection();
}
}
return false;
}
/// <summary>
/// 當(dāng)模型是對(duì)象,返回?fù)碛械膋ey
/// </summary>
/// <returns></returns>
public List<string> GetKeys()
{
if (!isModel)
return null;
List<string> list = new List<string>();
foreach (string subjson in base._GetCollection(this.rawjson))
{
string key = new CommonJsonModel(subjson).Key;
if (!string.IsNullOrEmpty(key))
list.Add(key);
}
return list;
}
/// <summary>
/// 當(dāng)模型是對(duì)象,key對(duì)應(yīng)是值,則返回key對(duì)應(yīng)的值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string GetValue(string key)
{
if (!isModel)
return null;
if (string.IsNullOrEmpty(key))
return null;
foreach (string subjson in base._GetCollection(this.rawjson))
{
CommonJsonModel model = new CommonJsonModel(subjson);
if (!model.IsValue())
continue;
if (model.Key == key)
return model.Value;
}
return null;
}
/// <summary>
/// 模型是對(duì)象,key對(duì)應(yīng)是對(duì)象,返回key對(duì)應(yīng)的對(duì)象
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public CommonJsonModel GetModel(string key)
{
if (!isModel)
return null;
if (string.IsNullOrEmpty(key))
return null;
foreach (string subjson in base._GetCollection(this.rawjson))
{
CommonJsonModel model = new CommonJsonModel(subjson);
if (!model.IsValue())
continue;
if (model.Key == key)
{
CommonJsonModel submodel = new CommonJsonModel(model.Value);
if (!submodel.IsModel())
return null;
else
return submodel;
}
}
return null;
}
/// <summary>
/// 模型是對(duì)象,key對(duì)應(yīng)是集合,返回集合
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public CommonJsonModel GetCollection(string key)
{
if (!isModel)
return null;
if (string.IsNullOrEmpty(key))
return null;
foreach (string subjson in base._GetCollection(this.rawjson))
{
CommonJsonModel model = new CommonJsonModel(subjson);
if (!model.IsValue())
continue;
if (model.Key == key)
{
CommonJsonModel submodel = new CommonJsonModel(model.Value);
if (!submodel.IsCollection())
return null;
else
return submodel;
}
}
return null;
}
/// <summary>
/// 模型是集合,返回自身
/// </summary>
/// <returns></returns>
public List<CommonJsonModel> GetCollection()
{
List<CommonJsonModel> list = new List<CommonJsonModel>();
if (IsValue())
return list;
foreach (string subjson in base._GetCollection(rawjson))
{
list.Add(new CommonJsonModel(subjson));
}
return list;
}
/// <summary>
/// 當(dāng)模型是值對(duì)象,返回key
/// </summary>
private string Key
{
get
{
if (IsValue())
return base._GetKey(rawjson);
return null;
}
}
/// <summary>
/// 當(dāng)模型是值對(duì)象,返回value
/// </summary>
private string Value
{
get
{
if (!IsValue())
return null;
return base._GetValue(rawjson);
}
}
}
}
使用方法
public CommonJsonModel DeSerialize(string json)
{
return new CommonJsonModel(json);
}
超級(jí)簡(jiǎn)單,只要new一個(gè)通用對(duì)象,把json字符串放進(jìn)去就行了。
針對(duì)上文的3個(gè)例子,我給出3種使用方法:
{"total":0}
CommonJsonModel model = DeSerialize(json);
model.GetValue("total") // return 0
{"total":0,"data":{"377149574" : 1}}
CommonJsonModel model = DeSerialize(json);
model.GetModel("data").GetValue("377149574") //return 1
{"total":0,"data":{"377149574":[{"cid":"377149574"}]}}
CommonJsonModel model = DeSerialize(json);
model.GetCollection("377149574").GetCollection()[0].GetValue("cid") //return 377149574
這個(gè)有點(diǎn)點(diǎn)復(fù)雜,
1. 首先377149574代表了一個(gè)集合,所以要用model.GetCollection("377149574")把這個(gè)集合取出來(lái)。
2. 其次這個(gè)集合里面包含了很多對(duì)象,因此用GetColllection()把這些對(duì)象取出來(lái)
3. 在這些對(duì)象List里面取第一個(gè)[0],表示取了":{"cid":"377149574"}這個(gè)對(duì)象,然后再用GetValue("cid")把對(duì)象的值取出來(lái)。
相關(guān)文章
asp.net GridView 刪除時(shí)彈出確認(rèn)對(duì)話框(包括內(nèi)容提示)
GridView 刪除時(shí)彈出確認(rèn)對(duì)話框(包括內(nèi)容提示)2009-12-12MVC4 基礎(chǔ) 枚舉生成 DropDownList 實(shí)用技巧
本篇文章小編為大家介紹,MVC4 基礎(chǔ) 枚舉生成 DropDownList 實(shí)用技巧。需要的朋友參考下2013-04-04ASP.NET簡(jiǎn)化編輯界面解決思路及實(shí)現(xiàn)代碼(2)
這篇與前一篇改進(jìn)部分,也許大家會(huì)留意到動(dòng)畫(huà)演示,主要是GridVeiw的更新與刪除會(huì)在每row都有。因此Insus.NET把它抽取出來(lái),放在GridView外,感興趣的朋友可以了解下啊,希望本文對(duì)你有所幫助2013-01-01記Asp.Net Core Swagger使用并帶域接口處理的方法
這篇文章主要介紹了記Asp.Net Core Swagger使用并帶域接口處理的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Asp.net中判斷一個(gè)session是否合法的方法
今天突然想到一個(gè)判斷session是否合法的做法,asp.net的,之前我們的做法是下面這樣的形式的:2013-07-07ASP.NET中DropDownList和ListBox實(shí)現(xiàn)兩級(jí)聯(lián)動(dòng)功能
這篇文章主要介紹了ASP.NET中DropDownList和ListBox實(shí)現(xiàn)兩級(jí)聯(lián)動(dòng)功能的相關(guān)資料,需要的朋友可以參考下2016-01-01Asp.net 彈出對(duì)話框基類(輸出alet警告框)
asp.net輸出alert警告框2008-11-11ASP.NET MVC5使用MiniProfiler監(jiān)控MVC性能
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC5使用MiniProfiler監(jiān)控MVC性能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07ASP.NET 計(jì)劃任務(wù)實(shí)現(xiàn)方法(不使用外接程序,.net內(nèi)部機(jī)制實(shí)現(xiàn))
在asp.net中要不使用其他插件的情況下只能使用定時(shí)器來(lái)檢查, 并執(zhí)行任務(wù).2011-09-09asp.net下將頁(yè)面內(nèi)容導(dǎo)入到word模板中的方法
asp.net下將頁(yè)面內(nèi)容導(dǎo)入到word模板中的方法,需要的朋友可以參考下。2010-10-10