public class JsonObject
{
//变量
protected System.Collections.Generic.Dictionary<String, Object> attributes;
public System.Collections.Generic.Dictionary<String, Object> Attributes
{
get { return attributes; }
set { attributes = value; }
}
/// <summary>
/// JsonObject 构造函数
/// </summary>
public JsonObject()
{
attributes = new Dictionary<String, Object>();
}
public JsonObject(string text)
{
attributes = new Dictionary<String, Object>();
if (!string.IsNullOrEmpty(text))
{
//parseJson
//解析
//object x = attributes.Keys;
}
}
public int Count
{
get { return attributes.Count; }
}
public Dictionary<String, Object>.KeyCollection Keys
{
get { return attributes.Keys; }
}
public Dictionary<String, Object>.ValueCollection Values
{
get { return attributes.Values; }
}
/// <summary>
/// 包装
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool ContainsKey(string key)
{
return this.attributes.ContainsKey(key);
}
/// <summary>
/// 设置字符属性值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void setAttribute(string key, string value)
{
if (attributes.ContainsKey(key))
{
attributes[key] = value;
}
else
{
attributes.Add(key, value);
}
}
/// <summary>
/// 设置bool属性值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void setAttribute(string key, bool value)
{
if (attributes.ContainsKey(key))
{
attributes[key] = value;
}
else
{
attributes.Add(key, value);
}
}
/ <summary>
/ 设置数值属性值
/ </summary>
/ <param name="key"></param>
/ <param name="value"></param>
//public void setAttribute(string key, decimal value)
//{
// if (attributes.ContainsKey(key))
// {
// attributes[key] = value;
// }
// else
// {
// attributes.Add(key, value);
// }
//}
/// <summary>
/// 设置数值double属性值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void setAttribute(string key, double value)
{
if (attributes.ContainsKey(key))
{
attributes[key] = value;
}
else
{
attributes.Add(key, value);
}
}
/// <summary>
/// 设置object属性值
/// </summary>
/// <param name="key">键值</param>
/// <param name="value">属性值</param>
public void setAttribute(string key, JsonObject value)
{
if (attributes.ContainsKey(key))
{
attributes[key] = value;
}
else
{
attributes.Add(key, value);
}
}
/// <summary>
/// 设置array属性值
/// </summary>
/// <param name="key"></param>
/// <param name="array"></param>
public void setAttribute(string key, JsonObject[] array)
{
if (attributes.ContainsKey(key))
{
attributes[key] = array;
}
else
{
attributes.Add(key, array);
}
}
/// <summary>
/// 设置array属性值
/// </summary>
/// <param name="key"></param>
/// <param name="array"></param>
public void setAttribute(string key, DateTime time)
{
if (attributes.ContainsKey(key))
{
attributes[key] = time;
}
else
{
attributes.Add(key, time);
}
}
/// <summary>
/// setAttribute
/// </summary>
/// <param name="key"></param>
/// <param name="array"></param>
public void setAttribute(string key, object value)
{
switch ((value.GetType()).ToString())
{
case "System.Boolean":
case "System.Double":
case "Nature.BLL.Json.JsonObject":
case "Nature.BLL.Json.JsonArray":
case "Nature.BLL.Json.JsonObject[]":
case "System.String":
case "System.Datetime":
attributes.Add(key, value);
break;
default:
throw new FormatException("JsonObject:setAttribute illegal type of Attribute! " + (value.GetType()).ToString());
//break;
}
}
/// <summary>
/// 返回属性值
/// </summary>
/// <param name="key">键值</param>
/// <returns>返回属性值</returns>
public object getAttribute(string key)
{
return attributes[key];
}
public void removeAttribute(string key)
{
attributes.Remove(key);
}
/*
public void renameAttribute(string oldKey, string newKey) {
setAttribute(newKey, getAttribute(oldKey));
removeAttribute(oldKey);
}
*/
/// <summary>
/// 输出字符串
/// </summary>
/// <returns></returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
if (attributes.Count == 0)
{
return "null";
}
//string str;
sb.Append("{");
int i = 0;
foreach (Object key in attributes.Keys)
{
if (i > 0)
{
sb.Append(",");
}
sb.Append(JsonUtil.attributeToString(key.ToString(), attributes[key.ToString()]));
i++;
}
sb.Append("} ");
return sb.ToString();
}
}
{
//变量
protected System.Collections.Generic.Dictionary<String, Object> attributes;
public System.Collections.Generic.Dictionary<String, Object> Attributes
{
get { return attributes; }
set { attributes = value; }
}
/// <summary>
/// JsonObject 构造函数
/// </summary>
public JsonObject()
{
attributes = new Dictionary<String, Object>();
}
public JsonObject(string text)
{
attributes = new Dictionary<String, Object>();
if (!string.IsNullOrEmpty(text))
{
//parseJson
//解析
//object x = attributes.Keys;
}
}
public int Count
{
get { return attributes.Count; }
}
public Dictionary<String, Object>.KeyCollection Keys
{
get { return attributes.Keys; }
}
public Dictionary<String, Object>.ValueCollection Values
{
get { return attributes.Values; }
}
/// <summary>
/// 包装
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool ContainsKey(string key)
{
return this.attributes.ContainsKey(key);
}
/// <summary>
/// 设置字符属性值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void setAttribute(string key, string value)
{
if (attributes.ContainsKey(key))
{
attributes[key] = value;
}
else
{
attributes.Add(key, value);
}
}
/// <summary>
/// 设置bool属性值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void setAttribute(string key, bool value)
{
if (attributes.ContainsKey(key))
{
attributes[key] = value;
}
else
{
attributes.Add(key, value);
}
}
/ <summary>
/ 设置数值属性值
/ </summary>
/ <param name="key"></param>
/ <param name="value"></param>
//public void setAttribute(string key, decimal value)
//{
// if (attributes.ContainsKey(key))
// {
// attributes[key] = value;
// }
// else
// {
// attributes.Add(key, value);
// }
//}
/// <summary>
/// 设置数值double属性值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void setAttribute(string key, double value)
{
if (attributes.ContainsKey(key))
{
attributes[key] = value;
}
else
{
attributes.Add(key, value);
}
}
/// <summary>
/// 设置object属性值
/// </summary>
/// <param name="key">键值</param>
/// <param name="value">属性值</param>
public void setAttribute(string key, JsonObject value)
{
if (attributes.ContainsKey(key))
{
attributes[key] = value;
}
else
{
attributes.Add(key, value);
}
}
/// <summary>
/// 设置array属性值
/// </summary>
/// <param name="key"></param>
/// <param name="array"></param>
public void setAttribute(string key, JsonObject[] array)
{
if (attributes.ContainsKey(key))
{
attributes[key] = array;
}
else
{
attributes.Add(key, array);
}
}
/// <summary>
/// 设置array属性值
/// </summary>
/// <param name="key"></param>
/// <param name="array"></param>
public void setAttribute(string key, DateTime time)
{
if (attributes.ContainsKey(key))
{
attributes[key] = time;
}
else
{
attributes.Add(key, time);
}
}
/// <summary>
/// setAttribute
/// </summary>
/// <param name="key"></param>
/// <param name="array"></param>
public void setAttribute(string key, object value)
{
switch ((value.GetType()).ToString())
{
case "System.Boolean":
case "System.Double":
case "Nature.BLL.Json.JsonObject":
case "Nature.BLL.Json.JsonArray":
case "Nature.BLL.Json.JsonObject[]":
case "System.String":
case "System.Datetime":
attributes.Add(key, value);
break;
default:
throw new FormatException("JsonObject:setAttribute illegal type of Attribute! " + (value.GetType()).ToString());
//break;
}
}
/// <summary>
/// 返回属性值
/// </summary>
/// <param name="key">键值</param>
/// <returns>返回属性值</returns>
public object getAttribute(string key)
{
return attributes[key];
}
public void removeAttribute(string key)
{
attributes.Remove(key);
}
/*
public void renameAttribute(string oldKey, string newKey) {
setAttribute(newKey, getAttribute(oldKey));
removeAttribute(oldKey);
}
*/
/// <summary>
/// 输出字符串
/// </summary>
/// <returns></returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
if (attributes.Count == 0)
{
return "null";
}
//string str;
sb.Append("{");
int i = 0;
foreach (Object key in attributes.Keys)
{
if (i > 0)
{
sb.Append(",");
}
sb.Append(JsonUtil.attributeToString(key.ToString(), attributes[key.ToString()]));
i++;
}
sb.Append("} ");
return sb.ToString();
}
}