json - 反序列化 json 无效属性名称

标签 json deserialization

从 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/

相关文章:

java - 将 CSV 格式的 JSON 转换为表格

java - 忽略 Jackson/spring/Java 中的 RootNode 和自定义映射

c# - 将 POST FromBody 转换为 HttpRequestMessage - 需要对请求正文进行反序列化

java - 尝试反序列化 DBEntity 时出现 ArangoDB、Java、Velocypack 错误

json - 在 scrapy 中将 json 参数传递给蜘蛛

c# - ASP.NET MVC JSON 将 HTML 表单发布到 View ?

javascript - 遍历json对象并根据键名将子obj属性移动到父对象

ios - Swift 3 - 错误 'Contextual type ' AnyObject' 不能与字典文字一起使用'

c# - XML 字符串到 c# 对象不返回数据

java - 如何使用 GSON 反序列化无 key 数组?