由于项目中需要取网页上的数据,但是数据时JSON格式的,于是听同事介绍了一个Newtonsoft.Json.dll文件
具体操作如下:
我的字符串类型为:
string
str =
"{ 'root':{ 'items': [{'Warehouse': '219410','City_St': 'Morrow,GA','Bales': '264'},{'Warehouse': '240124','City_St': 'Climax,'Bales': '88'},{'Warehouse': '266405','City_St': 'Funston,'Bales': '176'},{'Warehouse': '297005','City_St': 'Meigs,{'Warehouse': '710527','City_St': 'Greenville,SC','Bales': '440'}] }} "
;
|
需要循环取出Warehouse的值,这里我们使用了类构造函数
先定义所需要的类类型
然后就可以使用DecodeObject对象转换了
string str = "{ 'root':{ 'items': [{'Warehouse': '219410','Bales': '440'}] }} "; Root T = Utility.JsonUtility.DecodeObject<Root>(str); for (int i = 0; i < T.root.items.Count; i++) { Response.Write(T.root.items[i].Warehouse+\n); }
输出结果如下图
完整代码如下:
1 using System; 2 System.Collections.Generic; 3 System.Linq; 4 System.Web; 5 System.Web.UI; 6 System.Web.UI.WebControls; 7 Newtonsoft.Json; 8 Newtonsoft.Json.Converters; 9 System.IO; 10 11 public partial class _Default : System.Web.UI.Page 12 { 13 protected void Page_Load(object sender,EventArgs e) 14 { 15 ; 16 Root T = Utility.JsonUtility.DecodeObject<Root>(str); 17 ) 18 { 19 Response.Write(T.root.items[i].Warehouse+); 20 } 21 } 22 23 MyList 24 25 string Warehouse { get; set; } 26 string City_St { 27 28 Items 29 30 public IList<MyList> items { 31 32 Root 33 34 public Items root { 35 36 37 private static string StringFormat(string str,Type type) 38 39 if (type == typeof()) 40 41 str = String2Json(str); 42 str = \"" + str + 43 44 else typeof(DateTime)) 45 46 str = 47 48 bool49 50 str = str.ToLower(); 51 52 return str; 53 } 54 55 }
注:项目中的Newtonsoft是有3个版本的.NET2.0 、3.5、 4.0 要引用对应的版本才能找到名称空间;
Utility.cs完整代码如下:
@H_724_403@
@H_745_404@
@H_403_1185@
@H_301_1527@/// <param name="total"></param>
@H_403_1816@}
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
@H_315_502@
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
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
Newtonsoft.Json.Converters;
Newtonsoft.Json;
System.IO;
/// <summary>
///JsonUtility 的摘要说明
/// </summary>
Utility
{
/// <summary>
/// Json工具类
/// </summary>
static
JsonUtility
{
/// <summary>
/// 添加时间转换器
/// </summary>
/// <param name="serializer"></param>
private
void
AddIsoDateTimeConverter(JsonSerializer serializer)
{
IsoDateTimeConverter idtc =
new
IsoDateTimeConverter();
//定义时间转化格式
idtc.DateTimeFormat =
"yyyy-MM-dd HH:mm:ss"
;
//idtc.DateTimeFormat = "yyyy-MM-dd";
serializer.Converters.Add(idtc);
}
/// <summary>
/// Json转换配置
/// </summary>
/// <param name="serializer"></param>
SerializerSetting(JsonSerializer serializer)
{
serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
//serializer.NullValueHandling = NullValueHandling.Ignore;
//serializer.MissingMemberHandling = MissingMemberHandling.Ignore;
//serializer.DefaultValueHandling = DefaultValueHandling.Ignore;
}
/// <summary>
/// 返回结果消息编码
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sucess"></param>
/// <param name="message"></param>
/// <param name="exMessage"></param>
/// <param name="data"></param>
/// <returns></returns>
ReturnMessage(
bool
sucess,
int
total,monospace!important; min-height:inherit!important">message,monospace!important; min-height:inherit!important">exMessage,monospace!important; min-height:inherit!important">data)
{
message = message.Replace(
"'"
,
""
).Replace(
"\""
"<"
">"
);
exMessage = exMessage.Replace(
);
return
.Format(
"{{success:{0},total:{1},data:{2},message:\"{3}\",exMessage:\"{4}\"}}"
sucess.ToString().ToLower(),total,data,message,exMessage);
}
/// <summary>
/// 返回失败信息
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="message"></param>
/// <param name="exMessage"></param>
/// <returns></returns>
ReturnFailureMessage(
exMessage)
{
return
false
"[]"
);
}
/// <summary>
/// 返回失败信息
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="message"></param>
/// <param name="exMessage"></param>
/// <returns></returns>
ReturnFailureMessageTouch(
exMessage)
{
return
"{\"success\":\"false\",\"msg\":\""
+ exMessage +
"\"}"
;
}
/// <summary>
/// 返回成功信息
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="total"></param>
/// <param name="message"></param>
/// <param name="exMessage"></param>
/// <param name="objList"></param>
/// <returns></returns>
ReturnSuccessMessage<T>(
{
data = ListToJson<T>(objList);
true
}
ReturnSuccessMessageTouch<T>(T obj)
{
data = ObjectToJson<T>(obj);
data;
}
/// <summary>
/// 返回成功信息
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="total"></param>
/// <param name="message"></param>
/// <param name="exMessage"></param>
/// <param name="objList"></param>
/// <returns></returns>
ReturnSuccessMessage(
exMessage)
{
@H_560_1403@
);
}
/// <summary>
/// 返回成功信息
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="total"></param>
/// <param name="message"></param>
/// <param name="exMessage"></param>
/// <param name="objList"></param>
/// <returns></returns>
ReturnSuccessMessageTouch(
exMessage)
{
"{\"success\":\"true\",monospace!important; min-height:inherit!important">+ message +
;
}
/// <summary>
/// 返回成功信息
/// </summary>
/// <param name="message"></param>
/// <param name="exMessage"></param>
/// <param name="data">JSON 对象</param>
/// <returns></returns>
data)
{
"["
+ data +
"]"
);
}
/// <summary>
/// 返回成功消息
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="message"></param>
/// <param name="exMessage"></param>
/// <param name="obj"></param>
/// <returns></returns>
{
data = ObjectToJson<T>(obj);
}
/// <summary>
/// 把对象列表编码为Json数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="objList"></param>
/// <returns></returns>
ListToJson<T>(List<T> objList)
{
JsonSerializer serializer =
JsonSerializer();
SerializerSetting(serializer);
AddIsoDateTimeConverter(serializer);
(TextWriter sw =
StringWriter())
(JsonWriter writer =
JsonTextWriter(sw))
{
serializer.Serialize(writer,objList);
sw.ToString();
}
}
/// <summary>
/// 把一个对象编码为Json数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
ObjectToJson<T>(T obj)
{
JsonSerializer();
SerializerSetting(serializer);
AddIsoDateTimeConverter(serializer);
StringWriter())
JsonTextWriter(sw))
{
sw.ToString();
}
}
/// <summary>
/// 根据传入的Json数据,解码为对象(一个)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <returns></returns>
static
T DecodeObject<T>(
data)
{
JsonSerializer();
serializer.MissingMemberHandling = MissingMemberHandling.Ignore;
AddIsoDateTimeConverter(serializer);
StringReader sr =
StringReader(data);
(T)serializer.Deserialize(sr,255)!important">typeof
(T));
}
/// <summary>
/// 功能同DecodeObject
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <returns></returns>
List<T> DecodeObjectList<T>(
data)
{
JsonSerializer();
serializer.MissingMemberHandling = MissingMemberHandling.Ignore;
AddIsoDateTimeConverter(serializer);
StringReader(data);
(List<T>)serializer.Deserialize(sr,monospace!important; min-height:inherit!important">(List<T>));
EncodeAjaxResponseJson(
jsonString,monospace!important; min-height:inherit!important">callback)
{
String responseString =
;
//判断是否jsonp调用
if
(!String.IsNullOrEmpty(callback))
{
responseString = callback +
"("
+ jsonString +
")"
;
}
else
{
//普通ajax调用,直接返回Json数据
responseString = jsonString;
}
responseString;
}
ExtGridSortInfo(
property,monospace!important; min-height:inherit!important">direction)
{
"[{{\"property\":\"{0}\",\"direction\":\"{1}\"}}]"
}
}
}