您的位置:首页 > Web前端 > JavaScript

C# Json无组件纯代码解析

2014-03-17 08:44 375 查看
http://blog.sina.com.cn/s/blog_53c0699b01017cm7.html

JSON用处多多,但C#解析起来却有点麻烦,特别是旧版的C#2.0,网上的大部分JSON解析方法都是基于一些组件,而且有局限性,非得绑定实体。其实JSON解析没有这么复杂, 本文为无组件纯代码方式解析:

1、JSON对象

public class JsonObject

{

public Dictionary KeyValues = new Dictionary();

public Dictionary KeyArrays = new Dictionary();

public Dictionary KeyObject = new Dictionary();

}

2、解析文本

public class JsonParser

{

public List Jsons = new List();

public string ErrorMessage = "";

private enum JsonParseTypes

{

JsonObject,

JsonKey,

JsonValue,

JsonTextValue,

JsonObjectArray,

JsonTextArray

}

private Stack opStack = new Stack();

private Stack keyStack = new Stack();

private int jsonPos = 0;

private string jsonText = "";

public bool Parse(string text)

{

ErrorMessage = "";

Jsons = new List();

jsonText = text.Trim();

if (jsonText.Length < 2)

{

ErrorMessage = "Json text formate error.";

return false;

}

while (jsonPos < jsonText.Length)

{

char c = text[jsonPos];

if (c == '{')

{

// found objects

jsonPos++;

JsonObject jo = this.ParseJsonObject();

Jsons.Add(jo);

}

jsonPos++;

}

return true;

}

private JsonObject ParseJsonObject()

{

JsonObject result = new JsonObject();

string lastKey = "";

string lastValue = "";

bool QuatationClosed = true;

bool startKey = false;

bool startValue = false;

while (jsonPos < jsonText.Length)

{

char c = jsonText[jsonPos];

if (c == '{' && lastKey.Length > 0)

{

// found object

jsonPos++;

JsonObject jo = this.ParseJsonObject();

result.KeyObject.Add(lastKey, jo);

lastKey = "";

}

else if (c == '[' && lastKey.Length > 0)

{

// found arrays

jsonPos++;

List arr = this.ParseJsonArray();

result.KeyArrays.Add(lastKey, arr.ToArray());

lastKey = "";

}

else if (c == '\'' || c == '"')

{

if (lastKey.Length > 0 && startKey)

{

// end of value

startKey = false;

}

else if(!startValue)

{

// start key

startKey = true;

}

else if (startValue)

{

lastValue += c;

QuatationClosed = !QuatationClosed;

}

}

else if (c == ':' && QuatationClosed)

{

// start value

startValue = true;

}

else if (c == ',' && QuatationClosed && lastKey.Length > 0 )

{

// end of value

startValue = false;

result.KeyValues.Add(lastKey, ClearTextValue(lastValue));

lastKey = "";

lastValue = "";

}

else if (c == '}')

{

if (QuatationClosed && lastKey.Length > 0)

{

// end of value

startValue = false;

result.KeyValues.Add(lastKey, ClearTextValue(lastValue));

lastKey = "";

lastValue = "";

}

// end of object

break;

}

else

{

// key

if (startKey)

{

lastKey += c;

}

else if (startValue)

{

lastValue += c;

}

}

jsonPos++;

}

return result;

}

private List ParseJsonArray()

{

List result = new List();

string lastValue = "";

bool QuatationClosed = true;

while (jsonPos < jsonText.Length)

{

char c = jsonText[jsonPos];

if (c == '\'' || c == '"')

{

lastValue += c;

QuatationClosed = !QuatationClosed;

}

else if (c == ',' && QuatationClosed )

{

// end of value

result.Add(ClearTextValue(lastValue));

lastValue = "";

}

else if (c == ']')

{

if (QuatationClosed)

{

// end of value

result.Add(ClearTextValue(lastValue));

lastValue = "";

}

// end of object

break;

}

else

{

lastValue += c;

}

jsonPos++;

}

return result;

}

private string ClearTextValue(string value)

{

string text = "";

if (value.Length > 0)

{

text = value.Replace("\n", "").Trim();

if (text.Length > 0 && text.Substring(0, 1) == "'")

{

text = text.Substring(1);

if (text.Length > 0 && text.Substring(text.Length -1, 1) == "'")

{

text = text.Substring(0, text.Length -1);

}

}

else if (text.Length > 0 && text.Substring(0, 1) == "\"")

{

text = text.Substring(1);

if (text.Length > 0 && text.Substring(text.Length - 1, 1) == "\"")

{

text = text.Substring(0, text.Length - 1);

}

}

}

return text;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: