工作的时候写的一个JSON parser,因为.net的 System.JSON.dll死活import 不进来,就自己写了个。. 虽然很烂,但是不能随便用,有知识产权的呦O(∩_∩)O。仅供参考。
测试代码:
-
[TestMethod()]
-
public void ParserTest()
-
{
-
Console.WriteLine("xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
-
string content = string.Empty; // TODO: Initialize to an appropriate value
-
-
-
String file = "C:\\Users\\Administrator\\Desktop\\TestData\\test.txt";
-
StreamReader sr = new StreamReader(file);
-
content = sr.ReadToEnd();
-
JSONElement actual = new JSONElement(JsonUtils.Parser(content));
-
//actual.withBracket = true;
-
Console.WriteLine("xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
-
Console.WriteLine(actual.ToString());
-
-
//Assert.Inconclusive("Verify the correctness of this test method.");
-
}
JSONElement.cs
JsonValueItem
JSONUtils.cs
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
using System.IO;
-
-
namespace JSONMgr
-
{
-
class JsonUtils
-
{
-
private static void MoveFoward(String content, ref int index)
-
{
-
-
while (index < content.Length && (content[index] == ' ' || content[index] == '\r' || content[index] == '\n' || content[index] == ',' || content[index] == '\t'))
-
index++;
-
}
-
private static String TrimQuot(String input)
-
{
-
return input.Substring(1, input.Length - 2);
-
}
-
-
private static String readkey(String content, ref int index)
-
{
-
// if it is in string format {"key": "value"}
-
MoveFoward(content, ref index);
-
if (content[index] == '\"')
-
{
-
string ret = ReadStrValue(content, ref index);
-
return TrimQuot(ret);
-
}
-
//if it is in simple format {key: "value"}
-
StringBuilder sb = new StringBuilder();
-
while (content[index] != ' ' && content[index] != ':')
-
{
-
sb.Append(content[index++]);
-
}
-
-
return sb.ToString();
-
}
-
-
private static String ReadStrValue(string content, ref int index)
-
{
-
bool isString = false;
-
StringBuilder sb = new StringBuilder();
-
MoveFoward(content, ref index);
-
if (content[index] != '\"')
-
throw new Exception("Not a String");
-
sb.Append(content[index]);
-
-
char tmp = ' ';
-
isString = true;
-
while (isString)
-
{
-
index++;
-
sb.Append(content[index]);
-
if (tmp != '\\' && content[index] == '"')
-
isString = false;
-
tmp = content[index];
-
}
-
index++;
-
return sb.ToString();
-
}
-
-
private static JsonValueItem readvalue(String content, ref int index)
-
{
-
MoveFoward(content, ref index);
-
if (content[index] == '\"')
-
{
-
JsonValueItem jv = new JsonValueItem();
-
String strvalue = TrimQuot(ReadStrValue(content, ref index));
-
jv.StrValue = strvalue;
-
jv.ValueType = ItemType.Simple;
-
return jv;
-
}
-
else
-
{
-
JsonValueItem jv = Parser(content, ref index);
-
return jv;
-
}
-
-
}
-
-
private static JSONElement ParserItem(String content, ref int index)
-
{
-
MoveFoward(content, ref index);
-
if(content[index] == '{' || content[index] == '['){
-
JsonValueItem tmp = Parser(content, ref index);
-
return new JSONElement(tmp);
-
}
-
-
String key = readkey(content, ref index);
-
MoveFoward(content, ref index);
-
if (content[index] != ':')
-
{
-
throw new Exception("Should separate key and value with ':'");
-
}
-
index++;
-
MoveFoward(content, ref index);
-
JsonValueItem jv = readvalue(content, ref index);
-
MoveFoward(content, ref index);
-
return new JSONElement(key, jv);
-
}
-
-
private static JsonValueItem ParserArray(String content, ref int index)
-
{
-
-
index++;
-
List<JSONElement> ret = new List<JSONElement>();
-
while (true)
-
{
-
MoveFoward(content, ref index);
-
char cur = content[index];
-
if (cur == ']')
-
{
-
index++;
-
break;
-
}
-
ret.Add(ParserItem(content, ref index));
-
}
-
JsonValueItem jv = new JsonValueItem(ret, ItemType.Array);
-
-
return jv;
-
}
-
-
public static JsonValueItem Parser(String content)
-
{
-
int index = 0;
-
return Parser(content, ref index);
-
}
-
-
-
private static JsonValueItem ParserElement(String content, ref int index)
-
{
-
index++;
-
MoveFoward(content, ref index);
-
List<JSONElement> lj = new List<JSONElement>();
-
while (index < content.Length)
-
{
-
var tmp = ParserItem(content, ref index);
-
lj.Add(tmp);
-
MoveFoward(content, ref index);
-
if (content[index] == '}')
-
{
-
//withbracket = true;
-
index++;
-
break;
-
}
-
}
-
-
JsonValueItem ret = new JsonValueItem(lj,ItemType.Dict);
-
return ret;
-
}
-
-
-
private static JsonValueItem Parser(String content, ref int index)
-
{
-
MoveFoward(content, ref index);
-
if (content[index] != '{' && content[index] != '[')
-
throw new Exception("Unhandled!!!" + content[index]);
-
if (content[index] == '[')
-
return ParserArray(content, ref index);
-
if (content[index] == '{')
-
{
-
return ParserElement(content, ref index);
-
-
}
-
return null;
-
-
}
-
-
}
-
}
阅读(1912) | 评论(0) | 转发(0) |