当前位置:网站首页>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
边栏推荐
- leetcode--380.O(1) 时间插入、删除和获取随机元素
- 专题测试05·二重积分【李艳芳全程班】
- JS 力扣刷题 102. 二叉树的层序遍历
- Basic SQL query and learning
- JS 烧脑面试题大赏
- 2022年江西最新建筑八大员(质量员)模拟考试题库及答案解析
- Reading notes: meta matrix factorization for federated rating predictions
- freeCodeCamp----time_ Calculator exercise
- Search ideas and cases of large amount of Oracle redo log
- Influence of openssh version on SSH mutual trust creation in RAC environment
猜你喜欢
2022年江西最新建筑八大员(质量员)模拟考试题库及答案解析
MySQL [SQL performance analysis + SQL tuning]
Elmo (bilstm-crf + Elmo) (conll-2003 named entity recognition NER)
【报名】TF54:工程师成长地图与卓越研发组织打造
Strange bug of cnpm
Multithreading
Dolphin scheduler integrates Flink task pit records
Quartus Prime硬件实验开发(DE2-115板)实验二功能可调综合计时器设计
Static interface method calls are not supported at language level '5'
商家案例 | 运动健康APP用户促活怎么做?做好这几点足矣
随机推荐
Go语言 RPC通讯
3300万IOPS、39微秒延迟、碳足迹认证,谁在认真搞事情?
Question bank and answer analysis of the 2022 simulated examination of the latest eight members of Jiangxi construction (quality control)
Handling of high usage of Oracle undo
Express ② (routage)
联想产品经理林林:天津当地网络运营商网络故障 ZUI系统后台服务器暂时无法正常工作
Analysis of cluster component gpnp failed to start successfully in RAC environment
UML Unified Modeling Language
AttributeError: ‘dict‘ object has no attribute ‘iteritems‘
Force deduction brush question 101 Symmetric binary tree
神经元与神经网络
Choreographer全解析
[code analysis (2)] communication efficient learning of deep networks from decentralized data
记录一个奇怪的bug:缓存组件跳转之后出现组件复制
Port occupied 1
Reading notes: Secure federated matrix factorization
项目中遇到的问题(五)操作Excel接口Poi的理解
MySQL [read / write lock + table lock + row lock + mvcc]
SSM project deployed in Alibaba cloud
Static interface method calls are not supported at language level '5'