所以,我有以下格式的json文件
{
"-KeArK3V02mXYWx2OMWh" : {
"Description" : "this is a description",
"Location" : "Atlanta",
"Name" : "Event One",
"Time" : "2017-03-01T21:53:12.924645Z"
},
"-KeAtCNF_rmewZ_U3PpH" : {
"Description" : "another description",
"Location" : "Charlotte",
"Name" : "Event Two",
"Time" : "2017-03-01T22:01:25.603547Z"
},
"-KeAtd8CQW_EfH3Sw4YQ" : {
"Description" : "description goes here",
"Location" : "Toronto",
"Name" : "Event Three",
"Time" : "2017-03-01T22:03:19.3953859Z"
}
}
我有一个称为Event的类,其定义如下
class Event {
public string Description { get; set; }
public string Location { get; set; }
public string Name { get; set; }
public DateTime Time { get; set; }
}
我想通过这个过程,将每个子节点反序列化为Event对象,基本上将整个JSON反序列化为List 。
问题是事件不在数组中,它们是另一个JSON对象的子节点。事实证明,这并不像
List<Event> elist = JsonConvert.DeserializeObject<List<Event>>(jsonResult);
我已经看到类似的问题,这些问题在JSON数组中的组织位置,并且我尝试了在那里列出的解决方案,但它们仅在实际数组中起作用,而在这里没有结构。我在这里使用的是Google Firebase,不幸的是它不支持JSON数组,因此我无法将项目包含在数组中。
我不太习惯JSON语法,因此这里可能会遗漏一些确实很明显的内容,但是我完全感到困惑。
任何帮助将不胜感激。
最佳答案
您可以尝试这种方法吗?很简单
var str = @"{
'-KeArK3V02mXYWx2OMWh' : {
'Description' : 'this is a description',
'Location' : 'Atlanta',
'Name' : 'Event One',
'Time' : '2017-03-01T21:53:12.924645Z'
},
'-KeAtCNF_rmewZ_U3PpH' : {
'Description' : 'another description',
'Location' : 'Charlotte',
'Name' : 'Event Two',
'Time' : '2017-03-01T22:01:25.603547Z'
},
'-KeAtd8CQW_EfH3Sw4YQ' : {
'Description' : 'description goes here',
'Location' : 'Toronto',
'Name' : 'Event Three',
'Time' : '2017-03-01T22:03:19.3953859Z'
}
}";
Dictionary<string, Event> elist = JsonConvert.DeserializeObject<Dictionary<string, Event>>(str);
关于c# - 在C#中遍历JSON对象(不是数组),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42544062/