当前位置:网站首页>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
边栏推荐
- 服务器中挖矿病毒了,屮
- Special test 05 · double integral [Li Yanfang's whole class]
- Force deduction brush question 101 Symmetric binary tree
- Dolphin scheduler scheduling spark task stepping record
- Quartus Prime硬件实验开发(DE2-115板)实验二功能可调综合计时器设计
- L2-024 部落 (25 分)
- Leetcode brush question 𞓜 13 Roman numeral to integer
- leetcode--977. Squares of a Sorted Array
- Quartus prime hardware experimental development (de2-115 board) experiment II function adjustable comprehensive timer design
- Introduction to spark basic operation
猜你喜欢
Taobao released the baby prompt "your consumer protection deposit is insufficient, and the expiration protection has been started"
【vmware】vmware tools 地址
STM32学习记录0007——新建工程(基于寄存器版)
OSS cloud storage management practice (polite experience)
MySQL index [data structure + index creation principle]
Leetcode brush question 897 incremental sequential search tree
Express ② (routage)
Port occupied 1
Search ideas and cases of large amount of Oracle redo log
神经元与神经网络
随机推荐
自动化的艺术
Apache Atlas Compilation and installation records
Leetcode? The first common node of two linked lists
Decentralized Collaborative Learning Framework for Next POI Recommendation
美联储数字货币最新进展
Introduction to spark basic operation
Static interface method calls are not supported at language level '5'
UML Unified Modeling Language
JMeter pressure test tool
Choreographer全解析
AttributeError: ‘dict‘ object has no attribute ‘iteritems‘
Move blog to CSDN
Jenkins construction and use
L2-024 tribe (25 points)
Elmo (bilstm-crf + Elmo) (conll-2003 named entity recognition NER)
Wechat applet
Port occupied 1
FDFS start
JS 力扣刷题 102. 二叉树的层序遍历
Oracle RAC database instance startup exception analysis IPC send timeout