都市旷世高手,哥哥你好坏小游戏,狗狗网
概念介绍
还是先简单说说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源码:
/// <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")把对象的值取出来。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Net Core Web Api项目与在NginX下发布的方法
asp.net core3.1 引用的元包dll版本兼容性问题解决方案
IdentityServer4实现.Net Core API接口权限认证(快速入门)
ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径的实现
网友评论