gpt4 book ai didi

c# - 反序列化后 JSON 字段值变为 null

转载 作者:太空宇宙 更新时间:2023-11-03 22:33:50 27 4
gpt4 key购买 nike

我有一个像这样的 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 始终为空。但其他字段值已正确分配。任何人都可以解释这是什么原因。

最佳答案

我认为您正在使用 Newtonsoft.Json,它不会自动将带连字符的键名映射到 PascalCase 键名。

您可能没有注意到它,例如send-media 因为它不可为空/默认为 false。

如果不能改json,可以用JsonProperty修饰属性:

        [JsonProperty(PropertyName="send-media")]
public bool SendMedia { get; set; }
[JsonProperty(PropertyName="registration-token")]
public string RegistrationToken { get; set; }

关于c# - 反序列化后 JSON 字段值变为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56088401/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com