【发布时间】:2018-07-25 11:08:41
【问题描述】:
我有一个 JSON 字符串,我需要一些帮助来反序列化它。 目前我的结果始终为空。
var results = JsonConvert.DeserializeObject<Root>(json);
// result == null
我的 JSON:
{"First":{"FirstData1":{"date":"2018-01-01","hint":""},
"FirstData2":{"date":"2018-01-06","hint":""}},
"Second":{"SecondData1":{"date":"2018-01-01","hint":""},
"SecondData2":{"date":"2018-01-06","hint":""}}}....
只有在最后一个节点上才有实际的属性命名...
我的对象
public class Root
{
public IEnumerable<TempModelRoot> Value{ get; set; }
}
public class TempModelRoot
{
[JsonProperty("Key")]
public string Key { get; set; }
[JsonProperty("Value")]
public List<TempModelChild> Value { get; set; }
}
public class TempModelChild
{
[JsonProperty("Key")]
public string Key { get; set; }
[JsonProperty("Value")]
public TempModelInfo Value { get; set; }
}
public class TempModelInfo
{
[JsonProperty("date")]
public string date { get; set; }
[JsonProperty("hint")]
public string hint { get; set; }
}
【问题讨论】:
-
尝试将您的 json 插入 Quick type model generator 并查看您的模型与生成的模型相比如何。
-
我认为这是由于动态键(“First”、“Second”、“FirstData1”、“FirstData2”等)。您可以尝试在此问题中看到的使用 Dictionary 的建议:stackoverflow.com/questions/13517792/…
-
您的课程与提供的 json 不匹配。例如,Root 不是 IEnumerable。
-
@MXD 好链接!直接进入我的最爱,谢谢!
-
@MXD 为我工作。谢谢
标签: c# json json-deserialization