目标:以C#解析一串类似[{},{},{}]承载着数组数据的json字符串。将其反序列化为一个对象列表
运行环境:
NUnit2.4 C# .net2.0
代码:
1 //======================================================================
2 //
3 // Copyright (C) 2008-2009 PCODE TEAM. All rights reserved.
4 //
5 // CLR Version: 2.0.50727.1433
6 // NameSpace: TestPrj
7 // FileName: TestJson
8 //
9 // Created by Jy at 2009-3-19 22:17:15
10 // Email:jy@cjlu.edu.cn
11 //
12 //======================================================================
13
14 using System;
15 using System.Collections.Generic;
16 using System.Text;
17 using NUnit.Framework;
18 using Newtonsoft.Json;
19 namespace TestPrj
20 {
21 [TestFixture]
22 public class TestJson
23 {
24
25 [Test]
26 public void testA()
27 {
28 //测试数据
29 string ojson = "[{SZD:'BM',SGX:'=',SZ:'33'},{SZD:'SJ',SGX:'=',SZ:'3333'},{SZD:'SJ',SGX:'=',SZ:'3333'}]";
30
31 Newtonsoft.Json.JavaScriptArray jsa = (JavaScriptArray)JavaScriptConvert.DeserializeObject(ojson);
32 IList<searchArgs> li = new List<searchArgs>();
33 for (int i = 0; i < jsa.Count; i++)
34 {
35 JavaScriptObject jso = (JavaScriptObject)jsa[i];
36 searchArgs sa = new searchArgs(GetJsonStringValue(jso, "SZD"), GetJsonStringValue(jso, "SGX"), GetJsonStringValue(jso, "SZ"));
37 li.Add(sa);
38 }
39 System.Console.WriteLine(li.Count);
40 }
41 public class searchArgs {
42 public string SZD { get; set; }
43 public string SGX { get; set; }
44 public string SZ { get; set; }
45 public searchArgs(string szd,string sgx,string sz) {
46 SZD = szd;
47 SGX = sgx;
48 SZ = sz;
49 }
50 }
51 public static string GetJsonStringValue(Newtonsoft.Json.JavaScriptObject jso, string key)
52 {
53 try
54 {
55 return jso[key] == null ? string.Empty : jso[key].ToString();
56 }
57 catch (Exception e)
58 {
59 return string.Empty;
60 }
61 }
62 }
63 }
2 //
3 // Copyright (C) 2008-2009 PCODE TEAM. All rights reserved.
4 //
5 // CLR Version: 2.0.50727.1433
6 // NameSpace: TestPrj
7 // FileName: TestJson
8 //
9 // Created by Jy at 2009-3-19 22:17:15
10 // Email:jy@cjlu.edu.cn
11 //
12 //======================================================================
13
14 using System;
15 using System.Collections.Generic;
16 using System.Text;
17 using NUnit.Framework;
18 using Newtonsoft.Json;
19 namespace TestPrj
20 {
21 [TestFixture]
22 public class TestJson
23 {
24
25 [Test]
26 public void testA()
27 {
28 //测试数据
29 string ojson = "[{SZD:'BM',SGX:'=',SZ:'33'},{SZD:'SJ',SGX:'=',SZ:'3333'},{SZD:'SJ',SGX:'=',SZ:'3333'}]";
30
31 Newtonsoft.Json.JavaScriptArray jsa = (JavaScriptArray)JavaScriptConvert.DeserializeObject(ojson);
32 IList<searchArgs> li = new List<searchArgs>();
33 for (int i = 0; i < jsa.Count; i++)
34 {
35 JavaScriptObject jso = (JavaScriptObject)jsa[i];
36 searchArgs sa = new searchArgs(GetJsonStringValue(jso, "SZD"), GetJsonStringValue(jso, "SGX"), GetJsonStringValue(jso, "SZ"));
37 li.Add(sa);
38 }
39 System.Console.WriteLine(li.Count);
40 }
41 public class searchArgs {
42 public string SZD { get; set; }
43 public string SGX { get; set; }
44 public string SZ { get; set; }
45 public searchArgs(string szd,string sgx,string sz) {
46 SZD = szd;
47 SGX = sgx;
48 SZ = sz;
49 }
50 }
51 public static string GetJsonStringValue(Newtonsoft.Json.JavaScriptObject jso, string key)
52 {
53 try
54 {
55 return jso[key] == null ? string.Empty : jso[key].ToString();
56 }
57 catch (Exception e)
58 {
59 return string.Empty;
60 }
61 }
62 }
63 }
反序列化后的对象列表为li对象。输出反序列化后li对象中包含的对象个数用于核对。
Bingo!
:)