546 lines
21 KiB
C#
546 lines
21 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Text.RegularExpressions;
|
|||
|
|
|
|||
|
|
namespace CYQ.Data.Tool
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>ָ<EFBFBD>Json<6F>ַ<EFBFBD><D6B7><EFBFBD>Ϊ<EFBFBD>ֵ伯<D6B5>ϡ<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
internal partial class JsonSplit
|
|||
|
|
{
|
|||
|
|
internal static bool IsJson(string json)
|
|||
|
|
{
|
|||
|
|
int errIndex;
|
|||
|
|
return IsJson(json, out errIndex);
|
|||
|
|
}
|
|||
|
|
internal static bool IsJson(string json, out int errIndex)
|
|||
|
|
{
|
|||
|
|
errIndex = 0;
|
|||
|
|
|
|||
|
|
if (string.IsNullOrEmpty(json) || json.Length < 2 ||
|
|||
|
|
((json[0] != '{' && json[json.Length - 1] != '}') && (json[0] != '[' && json[json.Length - 1] != ']')))
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
CharState cs = new CharState();
|
|||
|
|
char c;
|
|||
|
|
for (int i = 0; i < json.Length; i++)
|
|||
|
|
{
|
|||
|
|
c = json[i];
|
|||
|
|
if (SetCharState(c, ref cs) && cs.childrenStart)//<2F><><EFBFBD>ùؼ<C3B9><D8BC><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC>
|
|||
|
|
{
|
|||
|
|
string item = json.Substring(i);
|
|||
|
|
int err;
|
|||
|
|
int length = GetValueLength(item, true, out err);
|
|||
|
|
cs.childrenStart = false;
|
|||
|
|
if (err > 0)
|
|||
|
|
{
|
|||
|
|
errIndex = i + err;
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
i = i + length - 1;
|
|||
|
|
}
|
|||
|
|
if (cs.isError)
|
|||
|
|
{
|
|||
|
|
errIndex = i;
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return !cs.arrayStart && !cs.jsonStart; //ֻҪ<D6BB><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>رգ<D8B1><D5A3><EFBFBD>ʧ<EFBFBD><CAA7>
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>Json
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="json"></param>
|
|||
|
|
/// <param name="op"><3E><><EFBFBD><EFBFBD>NO<4E><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEB6BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD>ǣ<EFBFBD>YES</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
internal static List<Dictionary<string, string>> Split(string json)
|
|||
|
|
{
|
|||
|
|
List<Dictionary<string, string>> result = new List<Dictionary<string, string>>();
|
|||
|
|
|
|||
|
|
if (!string.IsNullOrEmpty(json))
|
|||
|
|
{
|
|||
|
|
Dictionary<string, string> dic = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
|||
|
|
string key = string.Empty;
|
|||
|
|
StringBuilder value = new StringBuilder();
|
|||
|
|
CharState cs = new CharState();
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
char c;
|
|||
|
|
for (int i = 0; i < json.Length; i++)
|
|||
|
|
{
|
|||
|
|
c = json[i];
|
|||
|
|
if (!SetCharState(c, ref cs))//<2F><><EFBFBD>ùؼ<C3B9><D8BC><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC>
|
|||
|
|
{
|
|||
|
|
if (cs.jsonStart)//Json<6F><6E><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
if (cs.keyStart > 0)
|
|||
|
|
{
|
|||
|
|
key += c;
|
|||
|
|
}
|
|||
|
|
else if (cs.valueStart > 0)
|
|||
|
|
{
|
|||
|
|
value.Append(c);
|
|||
|
|
//value += c;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else if (!cs.arrayStart)//json<6F><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>飬<EFBFBD><E9A3AC><EFBFBD>˳<EFBFBD><CBB3><EFBFBD>
|
|||
|
|
{
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else if (cs.childrenStart)//<2F><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ֵ״̬<D7B4>¡<EFBFBD>
|
|||
|
|
{
|
|||
|
|
string item = json.Substring(i);
|
|||
|
|
int temp;
|
|||
|
|
int length = GetValueLength(item, false, out temp);
|
|||
|
|
//value = item.Substring(0, length);
|
|||
|
|
value.Length = 0;
|
|||
|
|
value.Append(item.Substring(0, length));
|
|||
|
|
cs.childrenStart = false;
|
|||
|
|
cs.valueStart = 0;
|
|||
|
|
//cs.state = 0;
|
|||
|
|
cs.setDicValue = true;
|
|||
|
|
i = i + length - 1;
|
|||
|
|
}
|
|||
|
|
if (cs.setDicValue)//<2F><><EFBFBD>ü<EFBFBD>ֵ<EFBFBD>ԡ<EFBFBD>
|
|||
|
|
{
|
|||
|
|
if (!string.IsNullOrEmpty(key) && !dic.ContainsKey(key))
|
|||
|
|
{
|
|||
|
|
//if (value != string.Empty)
|
|||
|
|
//{
|
|||
|
|
string val = value.ToString();
|
|||
|
|
bool isNull = json[i - 5] == ':' && json[i] != '"' && value.Length == 4 && val == "null";
|
|||
|
|
if (isNull)
|
|||
|
|
{
|
|||
|
|
val = "";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
dic.Add(key, val);
|
|||
|
|
|
|||
|
|
//}
|
|||
|
|
}
|
|||
|
|
cs.setDicValue = false;
|
|||
|
|
key = string.Empty;
|
|||
|
|
value.Length = 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (!cs.jsonStart && dic.Count > 0)
|
|||
|
|
{
|
|||
|
|
result.Add(dic);
|
|||
|
|
if (cs.arrayStart)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>顣
|
|||
|
|
{
|
|||
|
|
dic = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
catch (Exception err)
|
|||
|
|
{
|
|||
|
|
Log.Write(err, LogType.Error);
|
|||
|
|
}
|
|||
|
|
finally
|
|||
|
|
{
|
|||
|
|
key = null;
|
|||
|
|
value.Length = 0;
|
|||
|
|
value.Capacity = 16;
|
|||
|
|
value = null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡֵ<C8A1>ij<EFBFBD><C4B3>ȣ<EFBFBD><C8A3><EFBFBD>JsonֵǶ<D6B5><C7B6><EFBFBD><EFBFBD>"{"<22><>"["<22><>ͷʱ<CDB7><CAB1>
|
|||
|
|
/// </summary>
|
|||
|
|
private static int GetValueLength(string json, bool breakOnErr, out int errIndex)
|
|||
|
|
{
|
|||
|
|
errIndex = 0;
|
|||
|
|
int len = json.Length - 1;
|
|||
|
|
if (!string.IsNullOrEmpty(json))
|
|||
|
|
{
|
|||
|
|
CharState cs = new CharState();
|
|||
|
|
char c;
|
|||
|
|
for (int i = 0; i < json.Length; i++)
|
|||
|
|
{
|
|||
|
|
c = json[i];
|
|||
|
|
if (!SetCharState(c, ref cs))//<2F><><EFBFBD>ùؼ<C3B9><D8BC><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC>
|
|||
|
|
{
|
|||
|
|
if (!cs.jsonStart && !cs.arrayStart)//json<6F><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>飬<EFBFBD><E9A3AC><EFBFBD>˳<EFBFBD><CBB3><EFBFBD>
|
|||
|
|
{
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else if (cs.childrenStart)//<2F><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ֵ״̬<D7B4>¡<EFBFBD>
|
|||
|
|
{
|
|||
|
|
int length = GetValueLength(json.Substring(i), breakOnErr, out errIndex);//<2F>ݹ<EFBFBD><DDB9><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
cs.childrenStart = false;
|
|||
|
|
cs.valueStart = 0;
|
|||
|
|
//cs.state = 0;
|
|||
|
|
i = i + length - 1;
|
|||
|
|
}
|
|||
|
|
if (breakOnErr && cs.isError)
|
|||
|
|
{
|
|||
|
|
errIndex = i;
|
|||
|
|
return i;
|
|||
|
|
}
|
|||
|
|
if (!cs.jsonStart && !cs.arrayStart)//<2F><>¼<EFBFBD><C2BC>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>λ<EFBFBD>á<EFBFBD>
|
|||
|
|
{
|
|||
|
|
len = i + 1;//<2F><><EFBFBD>ȱ<EFBFBD><C8B1><EFBFBD><EFBFBD><EFBFBD>+1
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return len;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>ַ<EFBFBD>״̬
|
|||
|
|
/// </summary>
|
|||
|
|
private class CharState
|
|||
|
|
{
|
|||
|
|
internal bool jsonStart = false;//<2F><> "{"<22><>ʼ<EFBFBD><CABC>...
|
|||
|
|
internal bool setDicValue = false;// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>ֵ<EFBFBD>ˡ<EFBFBD>
|
|||
|
|
internal bool escapeChar = false;//<2F><>"\"ת<><D7AA><EFBFBD><EFBFBD><EFBFBD>ſ<EFBFBD>ʼ<EFBFBD><CABC>
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>鿪ʼ<E9BFAA><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ͷ<EFBFBD><CDB7><EFBFBD>㡿<EFBFBD><E3A1BF>ֵǶ<D6B5><EFBFBD><D7B5>ԡ<EFBFBD>childrenStart<72><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʶ<EFBFBD><CAB6>
|
|||
|
|
/// </summary>
|
|||
|
|
internal bool arrayStart = false;//<2F><>"[" <20><><EFBFBD>ſ<EFBFBD>ʼ<EFBFBD><CABC>
|
|||
|
|
internal bool childrenStart = false;//<2F>Ӽ<EFBFBD>Ƕ<EFBFBD><EFBFBD>ʼ<EFBFBD>ˡ<EFBFBD>
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>-1 δ<><CEB4>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0 ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD>1 ȡֵ<C8A1>С<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
internal int state = -1;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>-2 <20>ѽ<EFBFBD><D1BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-1 δ<><CEB4>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0 δ<><CEB4>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>1 <20><><EFBFBD><EFBFBD><EFBFBD>ſ<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>2 <20><><EFBFBD><EFBFBD><EFBFBD>ſ<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>3 ˫<><CBAB><EFBFBD>ſ<EFBFBD>ʼ<EFBFBD><CABC>
|
|||
|
|
/// </summary>
|
|||
|
|
internal int keyStart = -1;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>-2 <20>ѽ<EFBFBD><D1BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-1 δ<><CEB4>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0 δ<><CEB4>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>1 <20><><EFBFBD><EFBFBD><EFBFBD>ſ<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>2 <20><><EFBFBD><EFBFBD><EFBFBD>ſ<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>3 ˫<><CBAB><EFBFBD>ſ<EFBFBD>ʼ<EFBFBD><CABC>
|
|||
|
|
/// </summary>
|
|||
|
|
internal int valueStart = -1;
|
|||
|
|
|
|||
|
|
internal bool isError = false;//<2F>Ƿ<EFBFBD><C7B7><EFBFBD><EFB7A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|
|||
|
|
internal void CheckIsError(char c)//ֻ<><D6BB><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪGetLength<74><68><EFBFBD>ݹ鵽ÿһ<C3BF><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEB4A6><EFBFBD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
switch (c)
|
|||
|
|
{
|
|||
|
|
case '{'://[{ "[{A}]":[{"[{B}]":3,"m":"C"}]}]
|
|||
|
|
isError = jsonStart && state == 0;//<2F>ظ<EFBFBD><D8B8><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD> ͬʱ<CDAC><CAB1><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
break;
|
|||
|
|
case '}':
|
|||
|
|
isError = !jsonStart || (keyStart > 0 && state == 0);//<2F>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
break;
|
|||
|
|
case '[':
|
|||
|
|
isError = arrayStart && state == 0;//<2F>ظ<EFBFBD><D8B8><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
|||
|
|
break;
|
|||
|
|
case ']':
|
|||
|
|
isError = !arrayStart || (state == 1 && valueStart == 0);//<2F>ظ<EFBFBD><D8B8><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>[{},]1,0 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>[111,222] 1,1 [111,"22"] 1,-2
|
|||
|
|
break;
|
|||
|
|
case '"':
|
|||
|
|
isError = !jsonStart && !arrayStart;//δ<><CEB4>ʼJson<6F><6E>ͬʱҲδ<D2B2><CEB4>ʼ<EFBFBD><CABC><EFBFBD>顣
|
|||
|
|
break;
|
|||
|
|
case '\'':
|
|||
|
|
isError = !jsonStart && !arrayStart;//δ<><CEB4>ʼJson
|
|||
|
|
break;
|
|||
|
|
case ':':
|
|||
|
|
isError = (!jsonStart && !arrayStart) || (jsonStart && keyStart < 2 && valueStart < 2 && state == 1);//δ<><CEB4>ʼJson ͬʱ ֻ<>ܴ<EFBFBD><DCB4><EFBFBD><EFBFBD><EFBFBD>ȡֵ֮ǰ<D6AE><C7B0>
|
|||
|
|
break;
|
|||
|
|
case ',':
|
|||
|
|
isError = (!jsonStart && !arrayStart)
|
|||
|
|
|| (!jsonStart && arrayStart && state == -1) //[,111]
|
|||
|
|
|| (jsonStart && keyStart < 2 && valueStart < 2 && state == 0);//δ<><CEB4>ʼJson ͬʱ ֻ<>ܴ<EFBFBD><DCB4><EFBFBD><EFBFBD><EFBFBD>ȡֵ֮<D6B5><D6AE><EFBFBD><EFBFBD>
|
|||
|
|
break;
|
|||
|
|
default: //ֵ<><D6B5>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD>
|
|||
|
|
isError = (!jsonStart && !arrayStart) || (keyStart == 0 && valueStart == 0 && state == 0);//
|
|||
|
|
if (!isError && keyStart < 2)
|
|||
|
|
{
|
|||
|
|
if ((jsonStart && !arrayStart) && state != 1)
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ſ<EFBFBD>ͷ<EFBFBD>ģ<EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ {aaa:1}
|
|||
|
|
isError = c < 65 || (c > 90 && c < 97) || c > 122;
|
|||
|
|
}
|
|||
|
|
else if (!jsonStart && arrayStart && valueStart < 2)//
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ſ<EFBFBD>ͷ<EFBFBD>ģ<EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>[1]
|
|||
|
|
isError = c < 48 || c > 57;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
//if (isError)
|
|||
|
|
//{
|
|||
|
|
|
|||
|
|
//}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>״̬(<28><><EFBFBD><EFBFBD>true<75><65>Ϊ<EFBFBD>ؼ<EFBFBD><D8BC>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD><EFBFBD>false<73><65><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ͨ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
private static bool SetCharState(char c, ref CharState cs)
|
|||
|
|
{
|
|||
|
|
switch (c)
|
|||
|
|
{
|
|||
|
|
case '{'://[{ "[{A}]":[{"[{B}]":3,"m":"C"}]}]
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if (cs.keyStart <= 0 && cs.valueStart <= 0)
|
|||
|
|
{
|
|||
|
|
cs.CheckIsError(c);
|
|||
|
|
if (cs.jsonStart && cs.state == 1)
|
|||
|
|
{
|
|||
|
|
cs.valueStart = 0;
|
|||
|
|
cs.childrenStart = true;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
cs.state = 0;
|
|||
|
|
}
|
|||
|
|
cs.jsonStart = true;//<2F><>ʼ<EFBFBD><CABC>
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
break;
|
|||
|
|
case '}':
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ž<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if (cs.keyStart <= 0 && cs.valueStart < 2)
|
|||
|
|
{
|
|||
|
|
cs.CheckIsError(c);
|
|||
|
|
if (cs.jsonStart)
|
|||
|
|
{
|
|||
|
|
cs.jsonStart = false;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
cs.valueStart = -1;
|
|||
|
|
cs.state = 0;
|
|||
|
|
cs.setDicValue = true;
|
|||
|
|
}
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
// cs.isError = !cs.jsonStart && cs.state == 0;
|
|||
|
|
#endregion
|
|||
|
|
break;
|
|||
|
|
case '[':
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ſ<EFBFBD>ʼ
|
|||
|
|
if (!cs.jsonStart)
|
|||
|
|
{
|
|||
|
|
cs.CheckIsError(c);
|
|||
|
|
cs.arrayStart = true;
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
else if (cs.jsonStart && cs.state == 1 && cs.valueStart < 2)
|
|||
|
|
{
|
|||
|
|
cs.CheckIsError(c);
|
|||
|
|
//cs.valueStart = 1;
|
|||
|
|
cs.childrenStart = true;
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
break;
|
|||
|
|
case ']':
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ž<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if (!cs.jsonStart && (cs.keyStart <= 0 && cs.valueStart <= 0) || (cs.keyStart == -1 && cs.valueStart == 1))
|
|||
|
|
{
|
|||
|
|
cs.CheckIsError(c);
|
|||
|
|
if (cs.arrayStart)// && !cs.childrenStart
|
|||
|
|
{
|
|||
|
|
cs.arrayStart = false;
|
|||
|
|
}
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
break;
|
|||
|
|
case '"':
|
|||
|
|
case '\'':
|
|||
|
|
cs.CheckIsError(c);
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if (cs.jsonStart || cs.arrayStart)
|
|||
|
|
{
|
|||
|
|
if (!cs.jsonStart && cs.arrayStart)
|
|||
|
|
{
|
|||
|
|
cs.state = 1;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>飬ֻ<E9A3AC><D6BB>ȡֵ<C8A1><D6B5>û<EFBFBD><C3BB>Key<65><79><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0
|
|||
|
|
}
|
|||
|
|
if (cs.state == 0)//key<65><EFBFBD>
|
|||
|
|
{
|
|||
|
|
cs.keyStart = (cs.keyStart <= 0 ? (c == '"' ? 3 : 2) : -2);
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
else if (cs.state == 1)//ֵ<><EFBFBD>
|
|||
|
|
{
|
|||
|
|
if (cs.valueStart <= 0)
|
|||
|
|
{
|
|||
|
|
cs.valueStart = (c == '"' ? 3 : 2);
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
else if ((cs.valueStart == 2 && c == '\'') || (cs.valueStart == 3 && c == '"'))
|
|||
|
|
{
|
|||
|
|
if (!cs.escapeChar)
|
|||
|
|
{
|
|||
|
|
cs.valueStart = -2;
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
cs.escapeChar = false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
break;
|
|||
|
|
case ':':
|
|||
|
|
cs.CheckIsError(c);
|
|||
|
|
#region ð<EFBFBD><EFBFBD>
|
|||
|
|
if (cs.jsonStart && cs.keyStart < 2 && cs.valueStart < 2 && cs.state == 0)
|
|||
|
|
{
|
|||
|
|
cs.keyStart = 0;
|
|||
|
|
cs.state = 1;
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
break;
|
|||
|
|
case ',':
|
|||
|
|
cs.CheckIsError(c);
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD> {"a": [11,"22", ], "Type": 2}
|
|||
|
|
if (cs.jsonStart && cs.keyStart < 2 && cs.valueStart < 2 && cs.state == 1)
|
|||
|
|
{
|
|||
|
|
cs.state = 0;
|
|||
|
|
cs.valueStart = 0;
|
|||
|
|
cs.setDicValue = true;
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
else if (cs.arrayStart && !cs.jsonStart) //[a,b] [",",33] [{},{}]
|
|||
|
|
{
|
|||
|
|
if ((cs.state == -1 && cs.valueStart == -1) || (cs.valueStart < 2 && cs.state == 1))
|
|||
|
|
{
|
|||
|
|
cs.valueStart = 0;
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
break;
|
|||
|
|
case ' ':
|
|||
|
|
case '\r':
|
|||
|
|
case '\n':
|
|||
|
|
case '\t':
|
|||
|
|
if (cs.jsonStart && cs.keyStart <= 0 && cs.valueStart <= 0)
|
|||
|
|
{
|
|||
|
|
return true;//<2F><><EFBFBD><EFBFBD><EFBFBD>ո<EFBFBD><D5B8><EFBFBD>
|
|||
|
|
}
|
|||
|
|
break;
|
|||
|
|
default: //ֵ<><D6B5>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD>
|
|||
|
|
cs.CheckIsError(c);
|
|||
|
|
if (c == '\\') //ת<><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
if (cs.escapeChar)
|
|||
|
|
{
|
|||
|
|
cs.escapeChar = false;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
cs.escapeChar = true;
|
|||
|
|
//return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
cs.escapeChar = false;
|
|||
|
|
}
|
|||
|
|
if (cs.jsonStart)
|
|||
|
|
{
|
|||
|
|
if (cs.keyStart <= 0 && cs.state <= 0)
|
|||
|
|
{
|
|||
|
|
cs.keyStart = 1;//<2F><><EFBFBD><EFBFBD><EFBFBD>ŵ<EFBFBD>
|
|||
|
|
}
|
|||
|
|
else if (cs.valueStart <= 0 && cs.state == 1)
|
|||
|
|
{
|
|||
|
|
cs.valueStart = 1;//<2F><><EFBFBD><EFBFBD><EFBFBD>ŵ<EFBFBD>
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else if (cs.arrayStart)
|
|||
|
|
{
|
|||
|
|
cs.state = 1;
|
|||
|
|
if (cs.valueStart < 1)
|
|||
|
|
{
|
|||
|
|
cs.valueStart = 1;//<2F><><EFBFBD><EFBFBD><EFBFBD>ŵ<EFBFBD>
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
internal partial class JsonSplit
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>json<6F><6E><EFBFBD><EFBFBD><EFBFBD>ֳ<EFBFBD><D6B3>ַ<EFBFBD><D6B7><EFBFBD>List
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="jsonArray">["a,","bbb,,"]</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
internal static List<string> SplitEscapeArray(string jsonArray)
|
|||
|
|
{
|
|||
|
|
if (!string.IsNullOrEmpty(jsonArray))
|
|||
|
|
{
|
|||
|
|
jsonArray = jsonArray.Trim(' ', '[', ']');//["a,","bbb,,"]
|
|||
|
|
List<string> list = new List<string>();
|
|||
|
|
if (jsonArray.Length > 0)
|
|||
|
|
{
|
|||
|
|
string[] items = jsonArray.Split(',');
|
|||
|
|
string objStr = string.Empty;
|
|||
|
|
foreach (string item in items)
|
|||
|
|
{
|
|||
|
|
if (objStr == string.Empty) { objStr = item; }
|
|||
|
|
else { objStr += "," + item; }
|
|||
|
|
char firstChar = objStr[0];
|
|||
|
|
if (firstChar == '"' || firstChar == '\'')
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>˫<EFBFBD><CBAB><EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if (GetCharCount(objStr, firstChar) % 2 == 0)//<2F><><EFBFBD>ų<EFBFBD>˫
|
|||
|
|
{
|
|||
|
|
list.Add(objStr.Trim(firstChar).Replace("\\" + firstChar, firstChar.ToString()));
|
|||
|
|
objStr = string.Empty;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
list.Add(item);
|
|||
|
|
objStr = string.Empty;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return list;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵĴ<D6B5><C4B4><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="c"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
private static int GetCharCount(string item, char c)
|
|||
|
|
{
|
|||
|
|
int num = 0;
|
|||
|
|
for (int i = 0; i < item.Length; i++)
|
|||
|
|
{
|
|||
|
|
if (item[i] == '\\')
|
|||
|
|
{
|
|||
|
|
i++;
|
|||
|
|
}
|
|||
|
|
else if (item[i] == c)
|
|||
|
|
{
|
|||
|
|
num++;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return num;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|