当前位置:网站首页>json反序列化匿名数组/对象
json反序列化匿名数组/对象
2022-04-23 13:59:00 【流苏1990】
1、反序列化匿名数组
var result = "{\"batchNum\":\"20211130042359542\",\"responseStatus\":\"SUCCESS\",\"responseMessage\":null,\"executeResult\":\"SUCCESS\",\"restResponseDtlDTOList\":[{\"documentId\":null,\"documentCode\":\"CG080120211126002\",\"responseStatus\":\"SUCCESS\",\"responseMessage\":null},{\"documentId\":null,\"documentCode\":\"CG080120211129002\",\"responseStatus\":\"SUCCESS\",\"responseMessage\":null}]}";
JObject jsonObject = result.ToJObject();
var responseList = "{\"restResponseDtlDTOList\":" + jsonObject["restResponseDtlDTOList"].ToString() + "}";
var definition = new { restResponseDtlDTOList = new[] { new { documentID = "", documentCode = "", responseStatus = "", responseMessage = "" } } };
var encryptObject = JsonConvert.DeserializeAnonymousType(responseList, definition);
var ssss = "";
foreach (var responseItem in encryptObject.restResponseDtlDTOList)
{
if (responseItem.responseStatus.Equals("SUCCESS", StringComparison.OrdinalIgnoreCase))
continue;
ssss = "werwerwer";
}
System.Diagnostics.Debug.Write(ssss);
2、反序列化匿名对象
var definition = new { Name = "", Age = 0, Major = "" };
string json1 = @"{'Name':'GongHui','Age':28,'Major':'.NET ENGINEER'}";
var person1 = JsonConvert.DeserializeAnonymousType(json1, definition);
3、System.Text.Json的情况下:
/// <summary>
/// 反序列化为匿名对象
/// </summary>
/// <returns></returns>
[return: MaybeNull]
public static T DeserializeAnonymousType<T>(string json, T anonymousTypeObject, JsonSerializerOptions? options = null)
{
return JsonSerializer.Deserialize<T>(json, options: options);
}
/// <summary>
/// 反序列化为匿名对象
/// </summary>
/// <returns></returns>
[return: MaybeNull]
public static IEnumerable<T> DeserializeListAnonymousType<T>(string json, T anonymousTypeObject, JsonSerializerOptions? options = null)
{
return JsonSerializer.Deserialize<List<T>>(json, options: options);
}
版权声明
本文为[流苏1990]所创,转载请带上原文链接,感谢
https://blog.csdn.net/fuweiping/article/details/122210928
边栏推荐
- Small case of web login (including verification code login)
- Atcoder beginer contest 248c dice sum (generating function)
- Pytorch 经典卷积神经网络 LeNet
- 力扣刷题 101. 对称二叉树
- 函数只执行第一次的执行一次 once函数
- 19c RAC steps for modifying VIP and scanip - same network segment
- UML Unified Modeling Language
- JS 力扣刷题 102. 二叉树的层序遍历
- Leetcode | 38 appearance array
- Jiannanchun understood the word game
猜你喜欢
The art of automation
Port occupied 1
Quartus Prime硬件实验开发(DE2-115板)实验一CPU指令运算器设计
MySQL [SQL performance analysis + SQL tuning]
Pytorch 经典卷积神经网络 LeNet
Apache Atlas Compilation and installation records
Oracle告警日志alert.log和跟踪trace文件中文乱码显示
Kettle--控件解析
10g database cannot be started when using large memory host
Express ② (routage)
随机推荐
Quartus Prime硬件实验开发(DE2-115板)实验一CPU指令运算器设计
蓝绿发布、滚动发布、灰度发布,有什么区别?
[code analysis (2)] communication efficient learning of deep networks from decentralized data
OSS cloud storage management practice (polite experience)
redis如何解决缓存雪崩、缓存击穿和缓存穿透问题
cnpm的诡异bug
Modify the Jupiter notebook style
Business case | how to promote the activity of sports and health app users? It is enough to do these points well
Tensorflow Download
[VMware] address of VMware Tools
Express②(路由)
Port occupied 1
SSM project deployed in Alibaba cloud
Atcoder beginer contest 248c dice sum (generating function)
解决方案架构师的小锦囊 - 架构图的 5 种类型
Wechat applet
Question bank and answer analysis of the 2022 simulated examination of the latest eight members of Jiangxi construction (quality control)
Building MySQL environment under Ubuntu & getting to know SQL
商家案例 | 运动健康APP用户促活怎么做?做好这几点足矣
趣谈网络协议