C#JsonObjectJson格式与Json对象相互转换
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
/// JsonObject leedoom@qq
namespace JsonObject
{
public enum JsonItemType
{
String,
Array,
Integer,
Node
}
public sealed class Json
{
#region -- Private Fields --
private IList<JsonItem> _col;
private int _deep = 0;
private Json _parent;
#endregion
typeof array#region -- Methods --
public Json()
{
this._col = new List<JsonItem>();
}
public void Add(string key, string value)
{
if (key == null || key == "" || value == null) throw new ArgumentException("键值不能为空,参数不能为null");
JsonItem si = new JsonItem();
si.ItemType = JsonItemType.String;
si.Key = key;
si.Value = value;
this._col.Add(si);
}
public void Add(string key, int value)
{
if (key == null || key == "") throw new ArgumentException("键值不能为空,参数不能为null");
JsonItem si = new JsonItem();
si.ItemType = JsonItemType.Integer;
si.Key = key;
si.Value = value;
this._col.Add(si);
}
public void Add(string key, IList<object> value)
{
if (key == null || key == "" || value == null) throw new ArgumentException("键值不能为空,参数不能为null");
JsonItem si = new JsonItem();
si.ItemType = JsonItemType.Array;
si.Key = key;
si.Value = value;
this._col.Add(si);
}
public void Add(string key, Json value)
{
if (key == null || key == "" || value == null) throw new ArgumentException("键值不能为空,参数不能为null");
if (key == null || key == "" || value == null) throw new ArgumentException("键值不能为空,参数不能为null");            JsonItem si = new JsonItem();
si.ItemType = JsonItemType.Node;
si.Key = key;
si.Value = value;
this._col.Add(si);
}
public override string ToString()
{
return new JsonConvert.Writer(this).ToString();
}
public static Json Parse(string jsonString)
{
return new JsonConvert.Reader(jsonString).ToJson();
}
#endregion
#region -- Properties --
public int Length
{
get { return this._col.Count; }
}
public Jszn this[string key]
{
get
{
foreach (JsonItem si in this._col)
{
if (si.Key == key)
{
return new Jszn(si);
}
}
throw new ArgumentException("没有到Key为 " + key + " 的数据序列");
}
}
internal JsonItem this[int index]
{
get
{
return (JsonItem)this._col[index];
}
}
public int Deep
{
get { return this._deep; }
set { this._deep = value; }
}
public Json Parent
{
get { return this._parent; }
set { this._parent = value; }
}
#endregion
}
public sealed class Jszn
{
#region -- Private Fields --
JsonItem _item;
#endregion
#region -- Method --
public Jszn(JsonItem item)
{
{
this._item = item;
}
public override string ToString()
{
return this._item.Value.ToString();
}
public int ToInt()
{
if (this._item.ItemType == JsonItemType.Integer)
{
return Convert.ToInt32(this._item.Value);
}
else
{
throw new ArgumentOutOfRangeException("该返回数据类型不应为数字整型 Integer");
}
}
public string[] ToArray()
{
if (this._item.ItemType == JsonItemType.Array)
{
return (string[])this._item.Value;
}
else
{
throw new ArgumentOutOfRangeException("该返回数据类型不应为数组型 Array");
}
}
#endregion
#region -- Properties --
public Jszn this[string key]
{
get
{
if (this._item.ItemType == JsonItemType.Node)
{
Json json = (Json)this._item.Value;
return json[key];
}
else
{
throw new ArgumentOutOfRangeException("该返回数据类型不应为数据节点类型 Node");                }
}
}
public Jszn this[int index]
{
get
{
if (this._item.ItemType == JsonItemType.Array)
{
IList<object> result = (IList<object>)this._item.Value;
if (result[index].GetType() == typeof(System.String))
{
JsonItem ji = new JsonItem();
ji.ItemType = JsonItemType.String;
ji.Key = "";
ji.Value = result[index];
return new Jszn(ji);
}
else if (result[index].GetType() == typeof(System.Collections.Generic.List<object>))
{
JsonItem ji = new JsonItem();
ji.ItemType = JsonItemType.Array;
ji.ItemType = JsonItemType.Array;
ji.Key = "";
ji.Value = result[index];
return new Jszn(ji);
}
else if (result[index].GetType() == typeof(System.Int32))
{
JsonItem ji = new JsonItem();
ji.ItemType = JsonItemType.Integer;
ji.Key = "";
ji.Value = result[index];
return new Jszn(ji);
}
else if (result[index].GetType() == typeof(JsonObject.Json))
{
JsonItem ji = new JsonItem();
ji.ItemType = JsonItemType.Node;
ji.Key = "";
ji.Value = result[index];
return new Jszn(ji);
}
else
{
throw new Exception("未知的返回类型!");
}
}
else
{
throw new ArgumentOutOfRangeException("该返回数据类型不应为数组类型 Array");                }
}
}
#endregion
}
public class JsonItem
{
#region -- Privtae Fields --
private string _key;
private JsonItemType _itemType;
private object _value;
#endregion
#region -- Properties --
public string Key
{
get { return this._key; }
set { this._key = value; }
}
public JsonItemType ItemType
{
get { return this._itemType; }
set { this._itemType = value; }
}
public object Value
{
get { return this._value; }
set { this._value = value; }
}
#endregion
}
public sealed class JsonConvert
{
public sealed class Reader
{
#region -- Privtae Fields --
private string _xml;
private int current;
#endregion
#region -- Method --
public Reader(string Xml)
{
this._xml = Xml;
this.current = 0;
}
public Json ToJson()
{
return EnumKey();
}
private Json EnumKey()
{
bool dump = false, enumKey = true;
string sKey = "", sValue = "";
IList<object> aValue;
int iValue = 0;
int KeyEnd = 0, KeyStart = 0, KeyCount = 0;                Json json = new Json();
if (this._xml[current] == '{')
{
this.Plus();
this.Skip();
while (enumKey)
{
#region  -- 取得Key --
if (this._xml[current] != '"') break;
this.Plus();
KeyStart = current;
dump = false;
while (!dump)
{
while (this._xml[current] != '"')
{
current++;
}
if (this._xml[current - 1] != '\\')
{
dump = true;
}
}
KeyEnd = current;
KeyCount = KeyEnd - KeyStart;
sKey = this.Read(KeyStart, KeyCount);                        this.Plus();
#endregion
#region -- Key Value 分割 --
if (this._xml[current] != ':')
{
if (this._xml[current] != ' ')
{
this.Skip();
if (this._xml[current] != ':')
{
break;
}
else
{
this.Plus();
this.Skip();
}

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