-
Notifications
You must be signed in to change notification settings - Fork 13
/
新建文本文档.txt
270 lines (227 loc) · 8.35 KB
/
新建文本文档.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
/*
* JSON 的解析一开始是打算找到一个正则表达式直接用的,但是后来发现正则需要递归,但C#不支持这种“正则递归”。
* 所以,我觉得自己用C# + 正则去解析JSON
*
*
* 定义:
* Object JSON最外层对象,空或者多个Members
* Members Object 的 所有成员,一个Pair或多个Members
* Pair string : value
* Array [],[elements]
* Elements 一个value,elments
*
* value string number true false null object array
*
* JSON 本质是一个Diconary<string, object>
* Members 本质是一个Pair[]
* Pair 本质是一个单个键值对 key value
*
* Array 本质是一个object[]
*
* 伪代码:
* bool ParseObject(string content,out Object object)
* bool ParseMembers(string content,out Members members)
* bool ParsePair(string content,out Pair pair)
* bool ParseValue(string content,object result)
*
* 数组
* bool ParseArray(string value,out JsonParseArray result)
*
* 基础
* bool ParseString(string value,out string result)
* bool ParseNumber(string value,out float result)
* bool ParseBool(string value,out bool result)
*/
namespace ConsoleApplication1
{
class Program
{
public const float PI = 3.14f;
private static string test = "\"Images/Sun.png\" 250 true";
static void Main(string[] args)
{
int a = 10;
int b = 4;
string sr;
float fr;
bool br;
string path = @"D:\用户目录\我的文档\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\example1.json";
string jsonContent = File.ReadAllText(path);
Dictionary<string, object> obj = ParseJSON(jsonContent);
Console.WriteLine(a / (float)b);
Console.ReadLine();
}
/// <summary>
/// 解析JSON入口
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
internal static Dictionary<string, object> ParseJSON(string json)
{
Match match = Regex.Match(json, regexObject);
if (match.Success)
{
string content = match.Groups[1].Value;
return ParseObject(ref content);
}
return null;
}
internal static Dictionary<string, object> ParseObject(ref string content)
{
//解析members
Pair[] members = ParseMembers(ref content);
//创建Object
Dictionary<string, object> result = new Dictionary<string, object>();
if (members != null)
{
for (int i = 0, l = members.Length; i < l; i++)
{
result.Add(members[i].key, members[i].value);
}
}
return result;
}
internal static Pair[] ParseMembers(ref string content)
{
List<Pair> membersList = new List<Pair>();
//判断Object类型
Pair[] pairsOfObject = ParsePairsOfObject(ref content);
membersList.AddRange(pairsOfObject);
//判断String类型
Pair[] pairsOfString = ParsePairsOfString(ref content);
membersList.AddRange(pairsOfString);
//判断Number类型
Pair[] pairsOfNumber = ParsePairsOfNumber(ref content);
membersList.AddRange(pairsOfNumber);
//判断Bool类型
Pair[] pairsOfBool = ParsePairsOfBool(ref content);
membersList.AddRange(pairsOfBool);
// membersList.AddRange(pairsOfObject);
return membersList.ToArray();
}
internal static object[] ParseArray(ref string content)
{
List<object> array = new List<object>();
//解析Object
MatchCollection matchObjects = MatchesAndRemove(ref content, regexObject);
for (int i = 0, l = matchObjects.Count; i < l; i++)
{
string s = matchObjects[i].Value;
array.Add( ParseObject(ref s) );
}
}
#region 正则表达式
//解析键值对
internal const string regexPairOf = @"\s*" + regexString + @"\s*:\s*";
internal const string regexPairOfObject = regexPairOf + regexObject;
internal const string regexPairOfArray = regexPairOf + regexArray;
internal const string regexPairOfString = regexPairOf + regexString;
internal const string regexPairOfNumber = regexPairOf + regexNumber;
internal const string regexPairOfBool = regexPairOf + regexBool;
//对象
internal const string regexObject = @"\{([^}]*)\}";
//数组
internal const string regexArray = @"\[([^\]]*)\]";
//基础类型
internal const string regexString = "\"([^\"]*)\"";//"[^"]*"
internal const string regexNumber = @"(-?(?=[1-9]|0(?!\d))\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)";
internal const string regexBool = "(true|false|null)";
#endregion
/// <summary>
/// 对象类型 对
/// </summary>
// internal const string regexPairOfObject = regexPair + regexObject;
#region 解析键值对
internal static Pair[] ParsePairsOfObject(ref string content)
{
Pair[] pairs = ParsePairs(ref content, regexPairOfObject);
//递归解析Object
for (int i = 0, l = pairs.Length; i < l; i++)
{
pairs[i].value = ParseObject(ref pairs[i].content);
}
return pairs;
}
internal static Pair[] ParsePairsOfString(ref string content)
{
Pair[] pairs = ParsePairs(ref content, regexPairOfString);
//content == value
for (int i = 0, l = pairs.Length; i < l; i++)
{
pairs[i].value = pairs[i].content;
}
return pairs;
}
internal static Pair[] ParsePairsOfNumber(ref string content)
{
Pair[] pairs = ParsePairs(ref content, regexPairOfNumber);
for (int i = 0, l = pairs.Length; i < l; i++)
{
pairs[i].value = float.Parse(pairs[i].content);
}
return pairs;
}
internal static Pair[] ParsePairsOfBool(ref string content)
{
Pair[] pairs = ParsePairs(ref content, regexPairOfBool);
for (int i = 0, l = pairs.Length; i < l; i++)
{
pairs[i].value = pairs[i].content == "true";
}
return pairs;
}
/// <summary>
/// 解析键值对基础(解析出key 和 content)
/// </summary>
/// <returns></returns>
internal static Pair[] ParsePairs(ref string content,string regex)
{
MatchCollection matches = Regex.Matches(content, regex);
int count = matches.Count;
Pair[] pairs = new Pair[count];
for (int i = 0 ; i < count; i++)
{
Match match = matches[i];
string key = match.Groups[1].Value;
string valueString = match.Groups[2].Value;
pairs[i] = new Pair(key, valueString);
}
//删除解析过的字段(避免重复解析)
content = Regex.Replace(content, regex, string.Empty);
return pairs;
}
#endregion
internal static MatchCollection MatchesAndRemove(ref string content,string regex)
{
MatchCollection matches = Regex.Matches(content, regex);
int count = matches.Count;
if (count > 0)
{
//删除解析过的字段(避免重复解析)
content = Regex.Replace(content, regex, string.Empty);
}
return matches;
}
}
/// <summary>
/// 键值对
/// </summary>
internal class Pair
{
public string key;//键
public string content;//原始文本内容
public object value;//转换后的值
public Pair(string key, string content)
{
this.key = key;
this.content = content;
}
}
}