在.net中应用Newtonsoft.Json对Json字串进行反序列化
目标:以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!
:)
有时候专注也能让人幸福,游戏读书莫不如此,专注之人必有动人之处。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端