我有一个像这样的JSON字符串

{
  "data": {
    "id": "f4ba528a54117950",
    "type": "password-requests",
    "links": {
      "self": "https://api.abc.com/api/v2/password-requests/f4ba528a54117950"
    },
    "attributes": {
      "login": "abc",
      "type": "agent",
      "send-media": false,
      "registration-token": "ced84635eba"
    }
  }
}


我的课是这样的

public  class SightCallResult
{
    public SightCallData data { get; set; }
}

public class SightCallData
{
    public string id { get; set; }
    public string type { get; set; }
    public Dictionary<string, string> links { get; set; }
    public AgentAttributes attributes { get; set; }

}

public class AgentAttributes
{
    public string Login { get; set; }
    public string Type { get; set; }
    public bool SendMedia { get; set; }
    public string RegistrationToken { get; set; }
}


这就是我反序列化我的字符串的方式

sightCallRslt = JsonConvert.DeserializeObject<SightCallResult>(resultMobileToken);
sightCallData = sightCallRslt.data;
agentAttributes = sightCallData.attributes;
Debug.WriteLine(agentAttributes.RegistrationToken);


但是RegistrationToken始终为null。但是其他字段值已正确分配。任何人都可以解释这是什么原因。

最佳答案

可以将attributes的类型更改为Dictionary<string, object>,或者如果您确定数量有限的可能属性,请使用JsonPropertyAttribute指定确切的名称:

[JsonProperty("registration-token")]
public string RegistrationToken { get; set; }

09-25 17:40