在C#中迭代JSON对象(不是数组) [英] Iterating over a JSON object (NOT an array) in C#

查看:164
本文介绍了在C#中迭代JSON对象(不是数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个以下格式的json文件

  {
-KeArK3V02mXYWx2OMWh:{$
Location:Atlanta,
Name:Event One,
Time:2017- 03-01T21:53:12.924645Z

-KeAtCNF_rmewZ_U3PpH:{
描述:另一种描述,
位置:夏洛特,
Name:Event Two,
Time:2017-03-01T22:01:25.603547Z
},
-KeAtd8CQW_EfH3Sw4YQ:{$
Location:Toronto,
Name:Event Three,
Time:2017-03 -01T22:03:19.3953859Z

}

一个名为Event的类定义如下:

$ p $ class Event {
public string Description {get;组; }
public string Location {get;组; }
public string Name {get;组; }
public DateTime Time {get;组; }
}

我想通过这个并反序列化每个孩子节点转换为Event对象,基本上将整个JSON反序列化为一个List< Event> ;.

问题是事件不在数组中,它们是子节点另一个JSON对象。所以事实并非如此简单。

  List< Event> elist = JsonConvert.DeserializeObject< List< Event>>(jsonResult); 

我已经看到类似的问题,问题是在JSON数组中组织的项目,尝试了那里列出的解决方案,但只有当它是一个真正的数组,而不是我在这里的结构。谷歌的Firebase是我在这里工作,不幸的是它不支持JSON数组,所以我没有办法在数组中包含项目。



我我真的不习惯使用JSON语法,所以我可能会在这里丢失一些非常明显的东西,但我完全被困住了。



任何帮助都将不胜感激。 >

解决方案

您可以尝试这种方法吗?这是非常简单的

  var str = @{
'-KeArK3V02mXYWx2OMWh':{
' ':'这是一个描述',
'Location':'Atlanta',
'Name':'Event One',
'Time':'2017-03-01T21:53 :12.924645Z'
},
'-KeAtCNF_rmewZ_U3PpH':{
'描述':'另一个描述',
'Location':'Charlotte',
'名称':'Event Two',
'Time':'2017-03-01T22:01:25.603547Z'
},
'-KeAtd8CQW_EfH3Sw4YQ':{
'说明':'description here here',
'Location':'Toronto',
'Name':'Event Three',
'Time':'2017-03-01T22:03: 19.3953859Z'
}
};
词典< string,Event> elist = JsonConvert.DeserializeObject< Dictionary< string,Event>>(str);


So, I have a json file in the following format

{
  "-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"
  }
}

and I have a class called Event that is defined as follows

class Event {
  public string Description { get; set; }
  public string Location { get; set; }
  public string Name { get; set; }
  public DateTime Time { get; set; }
}

and I'd like to go through this and deserialize each of the child nodes into Event objects, basically deserializing the entire JSON into a List<Event>.

The issue is that the events aren't in an array, they're child nodes of another JSON object. So it turns out it's not as simple as

List<Event> elist = JsonConvert.DeserializeObject<List<Event>>(jsonResult);

I've seen similar questions asked where the items were organized in a JSON array, and I've tried the solutions listed there but they only work when it's an actual array, not the structure I have here. Google Firebase is what I'm working with here and unfortunately it doesn't support JSON arrays, so I have no way of containing the items in an array instead.

I'm not really used to JSON syntax so I might be missing something really obvious here, but I'm completely stumped.

Any help would be greatly appreciated.

解决方案

Can you try this approach? It's pretty straight forward

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#中迭代JSON对象(不是数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆