【发布时间】:2018-08-03 00:14:44
【问题描述】:
我正在尝试找出一种更好的方法来处理我在这里进行的这个对象/字典难题。
基本上,我得到一个 JSON iResponse(休息锐利)并反序列化为字典。这很好用,但是我得到了一个庞大的字典,其中还包含对象和可能更多的字典?我有点困惑。
这是我的代码,它抓取响应并放入字典:
var TicketInfo = jss.Deserialize<Dictionary<dynamic, dynamic>>(ticketExistsJSON.Content);
这是我在控制台中得到的:
所以这是在我的 JiraTicketInfo 变量中。你可以看到它充满了键值对,但是这些键值对的值里面是另一个字典?在这个字典中包含我想要的键值对。我特别想要1号。
我终于找到了办法
字典 JiraTicketInfo = TicketInfo["issues"][0]["fields"];
var bumStatus = (object[])JiraTicketInfo["customfield_10004"];
var numStatusDict = (字典)bumStatus[0];
JiraOrg = numStatusDict["name"].ToString();
但我觉得有一个更简单的方法来获得这个,但我的大脑似乎无法理解多维字典
任何帮助将不胜感激!
我什至不是在寻找答案,只是在寻找答案的地方。谢谢你:)
这是我的 JSON 响应:
{
"expand": "names,schema",
"startAt": 0,
"maxResults": 1,
"total": 1,
"issues": [
{
"expand": "customfield_10087.properties,operations,versionedRepresentations,editmeta,changelog,renderedFields",
"id": "18293",
"self": "https://www.myjirahost.com/rest/api/2/issue/18293",
"key": "SS-2991",
"fields": {
"customfield_10070": null,
"customfield_10071": null,
"customfield_10072": null,
"customfield_10073": null,
"customfield_10074": null,
"customfield_10075": null,
"customfield_10089": null,
"customfield_10004": [
{
"id": "99",
"name": "Organization Name B",
"_links": {
"self": "https://www.myjirahost.com/rest/servicedeskapi/organization/99"
}
}
],
"environment": null,
"duedate": null
}
}
]
}
【问题讨论】:
-
向我们展示你的 JSON,可能有比
Dictionary<dynamic, dynamic>更好的结构,但很模糊 -
JIRA 几乎肯定会返回一致的 JSON 格式,这意味着您应该反序列化为适当的 C# 类。
-
Dictionary<dynamic, dynamic>哇,我早上的咖啡都没喝。基本上 99.9999% 的时间只是看到dynamic这个词就告诉我有什么问题或更好的方法 -
@TheGeneral - 是的,使用
dynamic就像进入星际迷航全息甲板并要求计算机关闭所有安全协议。 -
此链接可能会有所帮助:json2csharp.com。
标签: c# json dictionary object