1、ASPNET万能JSON解析器ASP.NET万能JSON解析器概念介绍还是先简单说说Json的一些例子吧。注意,以下概念是我自己定义的,可以参考.net里面的TYPE的模型设计如果有争议,欢迎提出来探讨!1.最简单:total:0total就是值,值是数值,等于02. 复杂点total:0,data:377149574 : 1total是值,data是对象,这个对象包含了377149574这个值,等于13. 最复杂total:0,data:377149574:cid:377149574total是值,data是对象,377149574是数组,这个数组包含了一些列的对象,例如cid:377149
2、574这个对象。有了以上的概念,就可以设计出通用的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 = r
3、awjson.Split(new char : ); if (jsons.Length 2) return rawjson; return jsons0.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
4、 (jsons.Length 2) return rawjson; StringBuilder builder = new StringBuilder(); for (int i = 1; i 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 v
5、alue; protected List _GetCollection(string rawjson) /, List list = new List(); if (string.IsNullOrEmpty(rawjson) return list; rawjson = rawjson.Trim(); StringBuilder builder = new StringBuilder(); int nestlevel = -1; int mnestlevel = -1; for (int i = 0; i 0) list.Add(builder.ToString(); return list;
6、 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 rawjs
7、on) 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
8、() 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) CommonJsonMo
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1