浅析ASP.NET万能JSON解析器
概念介绍
还是先简单说说json的一些例子吧。注意,以下概念是我自己定义的,可以参考.net里面的type的模型设计
如果有争议,欢迎提出来探讨!
1.最简单:
{"total":0}
total就是值,值是数值,等于0
2. 复杂点
{"total":0,"data":{"377149574" : 1}}
total是值,data是对象,这个对象包含了"377149574"这个值,等于1
3. 最复杂
{"total":0,"data":{"377149574":[{"cid":"377149574"}]}}
total是值,data是对象,377149574是数组,这个数组包含了一些列的对象,例如{"cid":"377149574"}这个对象。
有了以上的概念,就可以设计出通用的json模型了。
万能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>
/// 当模型是对象,返回拥有的key
/// </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>
/// 当模型是对象,key对应是值,则返回key对应的值
/// </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>
/// 模型是对象,key对应是对象,返回key对应的对象
/// </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>
/// 模型是对象,key对应是集合,返回集合
/// </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>
/// 当模型是值对象,返回key
/// </summary>
private string key
{
get
{
if (isvalue())
return base._getkey(rawjson);
return null;
}
}
/// <summary>
/// 当模型是值对象,返回value
/// </summary>
private string value
{
get
{
if (!isvalue())
return null;
return base._getvalue(rawjson);
}
}
}
}
使用方法
public commonjsonmodel deserialize(string json)
{
return new commonjsonmodel(json);
}
超级简单,只要new一个通用对象,把json字符串放进去就行了。
针对上文的3个例子,我给出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
这个有点点复杂,
1. 首先377149574代表了一个集合,所以要用model.getcollection("377149574")把这个集合取出来。
2. 其次这个集合里面包含了很多对象,因此用getcolllection()把这些对象取出来
3. 在这些对象list里面取第一个[0],表示取了":{"cid":"377149574"}这个对象,然后再用getvalue("cid")把对象的值取出来。
上一篇: mysql的udf编程之非阻塞超时重传
下一篇: java判断ftp目录是否存在的方法