在字典中向下遍历键以从已知键中获取值
本文关键字:获取 遍历 字典 | 更新日期: 2023-09-27 17:56:36
我有一个看起来像这样的Dictionary<string, object> testJson
:
{
"Data": {
"Request": {
"Id": "ADLFKJ9LKJADFJLSKLDJFAS",
"Ts": "Fri Apr 04 10:51:54 EDT 2014",
"Parameters": "advertisers=&responseFormat=json"
},
"Response": {
"Id": null,
"Ts": null
},
"StatusCode": "09",
"Message": "Processing."
}
}
我需要获取 Data
==> Request
==> Id
和 Data
==> StatusCode
和 Data
==> Message
的值。
到目前为止,我已经
var requestId = (from p in testJson
where p.Key.Contains("Id")
select p.Value)
.FirstOrDefault();
Console.WriteLine(requestId.ToString());
它似乎没有按照我预期的方式工作... requestId
正在返回null
.
谢谢。
>假设嵌套的 JSON 元素也是类型 Dictionary<string, object>
...
var data = (Dictionary<string, object>)testJson["Data"];
var request = (Dictionary<string, object>)testJson["Request"];
var message = (string)data["Message"];
var status code = (string)data["StatusCode"];
var id = (string)request["Id"];