/// <summary>
/// ⽤于构建属性值的回调
/// </summary>
/// <param name="Property"></param>
public delegate void SetProperties(JsonObject Property);
/// <summary>
/// JsonObject属性值类型
/// </summary>
public enum JsonPropertyType
{
String,
Object,
Array,
Number,
Bool,
Null
}
/// <summary>
/// JSON通⽤对象
/// </summary>
public class JsonObject
{
private Dictionary<String, JsonProperty> _property;
public JsonObject()
{
this._property = null;
}
public JsonObject(String jsonString)
{
this.Parse(ref jsonString);
}
public JsonObject(SetProperties callback)
{
if (callback != null)
{
callback(this);
}
}
/// <summary>
/// Json字符串解析
/// </summary>
/// <param name="jsonString"></param>
private void Parse(ref String jsonString)
{
int len = jsonString.Length;
if (String.IsNullOrEmpty(jsonString) || jsonString.Substring(0, 1) != "{" || jsonString.Substring(jsonString.Length - 1, 1) != "}")            {
throw new ArgumentException("传⼊⽂本不符合Json格式!" + jsonString);
}
Stack<Char> stack = new Stack<char>();
Stack<Char> stackType = new Stack<char>();
StringBuilder sb = new StringBuilder();
Char cur;
bool convert = false;
bool isValue = false;
JsonProperty last = null;
bool isString = false;
for (int i = 1; i <= len - 2; i++)
{
cur = jsonString[i];
if (cur == '}')
{
;
}
if (cur == ' ' && !isString)
{
;
}
else if ((cur == '\'' || cur == '\"') && !convert && stack.Count == 0 && !isValue)
{
sb.Length = 0;
stack.Push(cur);
}
else if ((cur == '\'' || cur == '\"') && !convert && stack.Count > 0 && stack.Peek() == cur && !isValue)                {
stack.Pop();
}
else if ( (cur == '[' || cur == '{') && stack.Count == 0)
{
stackType.Push(cur == '[' ? ']' : '}');
sb.Append(cur);
}
else if ((cur == ']' || cur == '}') && stack.Count == 0 && stackType.Peek() == cur)
{
stackType.Pop();
sb.Append(cur);
}
else if (cur == ':' && stack.Count == 0 && stackType.Count == 0 && !isValue)
{
last = new JsonProperty();
this[sb.ToString()] = last;
isValue = true;
sb.Length = 0;
}
else if (cur == ',' && stack.Count == 0 && stackType.Count == 0)
{
if (last != null)
{
String temp = sb.ToString();
last.Parse(ref temp);
}
isValue = false;
sb.Length = 0;
}
else
{
if (cur == '"')
{
isString = !isString;
}
sb.Append(cur);
}
}
if (sb.Length > 0 && last != null && last.Type == JsonPropertyType.Null)
{
String temp = sb.ToString();
last.Parse(ref temp);
}
}
/// <summary>
/// 获取属性
/// </summary>
/// <param name="PropertyName"></param>
/
// <returns></returns>
public JsonProperty this[String PropertyName]
{
get
{
JsonProperty result = null;
if (this._property != null && this._property.ContainsKey(PropertyName))
{
result = this._property[PropertyName];
}
return result;
}
set
{
if (this._property == null)
if (this._property == null)
{
this._property = new Dictionary<string, JsonProperty>(StringComparer.OrdinalIgnoreCase);                }
if (this._property.ContainsKey(PropertyName))
{
this._property[PropertyName] = value;
}
else
{
this._property.Add(PropertyName, value);
}
}
}
/// <summary>
/// 通过此泛型函数可直接获取指定类型属性的值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="PropertyName"></param>
/
// <returns></returns>
public virtual T Properties<T>(String PropertyName) where T : class
{
JsonProperty p = this[PropertyName];
if (p != null)
{
return p.GetValue<T>();
}
return default(T);
}
/// <summary>
/
// 获取属性名称列表
/// </summary>
/// <returns></returns>
public String[] GetPropertyNames()
{
if (this._property == null)
return null;
String[] keys = null;
if (this._property.Count > 0)
{
keys = new String[this._property.Count];
this._property.Keys.CopyTo(keys, 0);
}
return keys;
}
/// <summary>
/// 移除⼀个属性
/// </summary>
/// <param name="PropertyName"></param>
/// <returns></returns>
public JsonProperty RemoveProperty(String PropertyName)
{
if (this._property != null && this._property.ContainsKey(PropertyName))
{
JsonProperty p = this._property[PropertyName];
this._property.Remove(PropertyName);
return p;
}
return null;
}
/// <summary>
/// 是否为空对象
/// </summary>
/
// <returns></returns>
public bool IsNull()
{
return this._property == null;
}
public override string ToString()
public override string ToString()
{
return this.ToString("");
}
/// <summary>
/
//
/// </summary>
/// <param name="format">格式化字符串</param>
/// <returns></returns>
public virtual string ToString(String format)
{
if (this.IsNull())
{
return "{}";
}
else
{
StringBuilder sb = new StringBuilder();
foreach (String key in this._property.Keys)
{
sb.Append(",");
sb.Append("\"").Append(key).Append("\"").Append(": ");                    sb.Append(this._property[key].ToString(format));
}
if (this._property.Count > 0)
{
sb.Remove(0, 1);
}
sb.Insert(0, "{");
sb.Append("}");
return sb.ToString();
}
}
}
/// <summary>
/// JSON对象属性
/// </summary>
public class JsonProperty
{
private JsonPropertyType _type;
private String _value;
private JsonObject _object;
private List<JsonProperty> _list;
private bool _bool;
private double _number;
public JsonProperty()
{
this._type = JsonPropertyType.Null;
this._value = null;
this._object = null;
this._list = null;
}
public JsonProperty(Object value)
{
this.SetValue(value);
}
/// <summary>
/// Json字符串解析
/// </summary>
/// <param name="jsonString"></param>
public void Parse(ref String jsonString)
数组转换成字符串
{
if (String.IsNullOrEmpty(jsonString))
{
if (jsonString == "")
{
this.SetValue("");

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。