我已经提到了this question,它与我的问题类似,但是由于数据结构不同,因此无法完全解决该问题,并且我无法弄清楚如何将此解决方案应用于示例数据如下所示:
{
"result": {
"RITM2572913": {
"number": "RITM2572913",
"state": "1",
"stage": "fulfillment",
"Sys_ID": "220e89681b31b384e3a0a79b2d4bcbf3",
"requested_for": "1d1673c4dbda5b0072a85099dc9619b0",
"Contoso_requested_for": "requested_for:1d1673c4dbda5b0072a85099dc9619b0,var_name_arr:",
"Contoso_sc_Purposeofthef5request": "Add",
"Contoso_Sc_Contactinfo": "Contact ",
"Contoso_sc_Appname": "Application ",
"Contoso_sc_Description": "Description",
"Contoso_special_instructions": "special_instructions:",
"business_justification": "Justification ",
"Contoso_business_justification": "busess_justification:Justification",
"Contoso_catalog_item_footer": "owner_info:"
}
}
}
我有这样的响应数据,需要反序列化以适合下面给出的对象模型:
public class RITMGETRequestResponse
{
public RITMDetails result { get; set; }
public class RITMDetails
{
public string business_justification { get; set; }
public string number { get; set; }
public string requested_for { get; set; }
public string stage { get; set; }
public string state { get; set; }
public string Sys_ID { get; set; }
public string var_name_arr { get; set; }
public string Contoso_business_justification { get; set; }
public string Contoso_catalog_item_footer { get; set; }
public string Contoso_requested_for { get; set; }
public string Contoso_sc_Appname { get; set; }
public string Contoso_Sc_Contactinfo { get; set; }
public string Contoso_sc_Description { get; set; }
public string Contoso_sc_Purposeofthef5request { get; set; }
public string Contoso_special_instructions { get; set; }
}
}
在这种情况下,RITM号是动态的。我需要获取JSON的Sys_ID和其他属性。如何反序列化此JSON响应以获取这些值?
答案 0 :(得分:1)
简单的示例:
使用JSONProperty属性映射动态属性名称的结果值
class Program
{
static void Main(string[] args)
{
var deserialise = JsonConvert.DeserializeObject<RITMRequestResponse>("{\"result\": {\"123\" : { \"number\" : \"123\" }}}");
Console.WriteLine(deserialise);
Console.ReadLine();
}
}
public class RITMRequestResponse
{
[JsonProperty(PropertyName = "result")]
public Dictionary<string, RITMDetails> RITMDetails { get; set; }
}
public class RITMDetails
{
public string Number { get; set; }
}