Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从atlassian的Json响应中存储customfields,例如(customfield_10020),它在c中每次都是动态的_C#_Asp.net Web Api_Model View Controller - Fatal编程技术网

C# 如何从atlassian的Json响应中存储customfields,例如(customfield_10020),它在c中每次都是动态的

C# 如何从atlassian的Json响应中存储customfields,例如(customfield_10020),它在c中每次都是动态的,c#,asp.net-web-api,model-view-controller,C#,Asp.net Web Api,Model View Controller,下面是我从Api得到的响应,它是动态的,如图所示。我在customfield_10011键中获得了值。在另一个对象中,我得到customfield_10014键中的值。现在我应该如何循环它并用c保存数据 使用Json.Net将其序列化为动态文件非常简单 dynamic myDynamicJson = JsonConvert.DeserializeObject("{ 'environment': null, 'customfield_10027': "Multi Sensor Updated" }

下面是我从Api得到的响应,它是动态的,如图所示。我在customfield_10011键中获得了值。在另一个对象中,我得到customfield_10014键中的值。现在我应该如何循环它并用c保存数据


使用Json.Net将其序列化为动态文件非常简单

dynamic myDynamicJson = JsonConvert.DeserializeObject("{ 'environment': null, 'customfield_10027': "Multi Sensor Updated" }");

string customfield10027 = myDynamicJson.customfield_10027;
编辑

要从配置中获取字段名,我宁愿使用自定义解析器方法。但如果你不能:

dynamic myDynamicJson = JsonConvert.DeserializeObject("{ 'environment': null, 'customfield_10027': 'Multi Sensor Updated' }");
string myFieldNameFromConfig = "customfield_100273";

string result = myDynamicJson[myFieldNameFromConfig];

使用Json.Net将其序列化为动态文件非常简单

dynamic myDynamicJson = JsonConvert.DeserializeObject("{ 'environment': null, 'customfield_10027': "Multi Sensor Updated" }");

string customfield10027 = myDynamicJson.customfield_10027;
编辑

要从配置中获取字段名,我宁愿使用自定义解析器方法。但如果你不能:

dynamic myDynamicJson = JsonConvert.DeserializeObject("{ 'environment': null, 'customfield_10027': 'Multi Sensor Updated' }");
string myFieldNameFromConfig = "customfield_100273";

string result = myDynamicJson[myFieldNameFromConfig];

我对jiraapi也有类似的问题

如果您100%确信这些自定义属性不会更改,那么请务必使用动态路由

我一直在与多个Jira实例集成,它们对同一属性有不同的自定义字段,因此首选更流畅的映射,因为它可以从一些配置中动态完成

这可以通过JsonConvert的CustomContractResolver实现

例如:

 return JsonConvert.DeserializeObject<JiraIssuePayload>(str, new JsonSerializerSettings()
 {
     ContractResolver = new CustomContractResolver(new Dictionary<string, string>
     {
         {"EpicName", "customfield_10011" },
         {"Epic", "customfield_10012" }, // these could come from some configuration object
     });
 }

这里要做的是告诉JsonConvert将customfield_10011反序列化为您类型的属性EpicName,并将另一个类型的属性Epic

我对jiraapi也有类似的问题

如果您100%确信这些自定义属性不会更改,那么请务必使用动态路由

我一直在与多个Jira实例集成,它们对同一属性有不同的自定义字段,因此首选更流畅的映射,因为它可以从一些配置中动态完成

这可以通过JsonConvert的CustomContractResolver实现

例如:

 return JsonConvert.DeserializeObject<JiraIssuePayload>(str, new JsonSerializerSettings()
 {
     ContractResolver = new CustomContractResolver(new Dictionary<string, string>
     {
         {"EpicName", "customfield_10011" },
         {"Epic", "customfield_10012" }, // these could come from some configuration object
     });
 }

这里要做的是告诉JsonConvert将customfield_10011反序列化为您类型的属性EpicName,并将另一个类型的属性Epic

为什么不创建一个与JSON匹配的模型呢?@JonathanAlfaro,我已经尝试过了,但是我的关键更改来自开发URL和Live URL。所以我想让它从appsetting.JSON设置它为什么不创建一个与JSON匹配的模型?@JonathanAlfaro,我已经尝试过了,但是我的关键更改来自开发URL和Live URL。所以我想让它从appsetting.JSONin中设置,在开发模式下,它加载到customfield_10027中,但在实时模式下,它加载到customfield_10018中。现在我想在appsetting.json中设置这些,并从中消费。在开发模式下,它加载到customfield_10027中,但在实时模式下,它加载到customfield_10018中。现在我想在appsetting.json中设置这些,并从中消费。在开发模式下,它加载到customfield_10027中,但在实时模式下,它加载到customfield_10018中。现在我想在appsetting.json中设置它们并从中消费。@Vishvjitshinde然后我描述的方法就是这样做的。在开发模式下,它加载到customfield_10027中,但在实时模式下,它加载到customfield_10018中。现在我想在appsetting.json中设置这些参数并从中消费。@Vishvjitshinde然后我描述的方法就是这样做的。