从 Mailchimp 读取的部分 json 如下所示。
"interests": {
"5e0344ae18": true,
"545e5bdb01": true,
"33aa542ea0": true,
"f51a5f9716": true,
"31109a4d58": true,
"bf5f946fd4": true,
"563320981e": false
}
所以我认为用于反序列化的属性应该是这个。
public Interests interests { get; set; }
public class Interests
{
public bool 5e0344ae18 { get; set; }
public bool 545e5bdb01 { get; set; }
public bool 33aa542ea0 { get; set; }
public bool f51a5f9716 { get; set; }
public bool 31109a4d58 { get; set; }
public bool bf5f946fd4 { get; set; }
public bool 563320981e { get; set; }
}
但是,由数字和字母组成的属性名称无效,每个属性名称都会出现编译错误,例如“类、结构或接口(interface)成员声明中的无效标记“5e0344””。 属性名称如何与json数据中的名称匹配?
最佳答案
虽然我不相信您能够使用以数字开头的属性名称,但您可以在它们前面添加字符或字符串前缀,并进行一些手动解析。
假设您使用 C#,http://www.newtonsoft.com/json/help/html/t_newtonsoft_json_linq_jobject.htm可能允许您处理 JSON 响应并解析属性,而无需使用自动反序列化,我猜您当前正在使用。
这是我发现的另一篇文章,描述了类似的问题和一些潜在的解决方案:Parse jsonObject when field names are unknowm
关于json - 反序列化 json 无效属性名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39863768/