【问题标题】:How to read from Multi-Dimensional dictionary C#如何从多维字典 C# 中读取
【发布时间】: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&lt;dynamic, dynamic&gt; 更好的结构,但很模糊
  • JIRA 几乎肯定会返回一致的 JSON 格式,这意味着您应该反序列化为适当的 C# 类。
  • Dictionary&lt;dynamic, dynamic&gt; 哇,我早上的咖啡都没喝。基本上 99.9999% 的时间只是看到 dynamic 这个词就告诉我有什么问题或更好的方法
  • @TheGeneral - 是的,使用dynamic 就像进入星际迷航全息甲板并要求计算机关闭所有安全协议。
  • 此链接可能会有所帮助:json2csharp.com

标签: c# json dictionary object


【解决方案1】:

在大家的帮助下使用 resttosharp 网站和构建类解决了:

RootObject JiraIssueObj = JsonConvert.DeserializeObject(ticketExistsJSON.Content);

【讨论】:

    猜你喜欢
    • 2018-08-27
    • 2018-03-22
    • 2015-02-23
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    相关资源
    最近更新 更多