当Json响应的节点的日期不断变化时,我该如何解析?

时间:2019-07-11 23:49:55

标签: c# json parsing

我正在尝试使用C#解析Json响应,但是父节点之一是类似于“ 2019-07-11”的日期。通常,我只会创建类以将响应反序列化为该类,但是此date节点使我陷入循环。

这就是我在做什么。我收到JSON响应,并尝试将其反序列化为不错的类:

   rc.EndPoint = "https://api.nasa.gov/neo/rest/v1/feed?api_key=xxxxxxxxxxxx";
   rc.Method = HttpVerb.GET;
   response = rc.MakeRequest();
   r = JsonConvert.DeserializeObject<RootAsteroidObject>(response);

这是我的课程:

public class RootAsteroidObject
   {
        public Links links { get; set; }
        public int element_count { get; set; }
        public NearEarthObjects near_earth_objects { get; set; }
   }
   public class NearEarthObjects
   {
        public string asteroid { get; set; }
   }

   public class AsteroidInfo
   {
       public string id { get; set; }
       public string neo_reference_id { get; set; }
       public string name { get; set; }
       public string nasa_jpl_url { get; set; }
       public double absolute_magnitude_h { get; set; }
   }

在解析后,在我的RootAsteroidOjbect对象中,它显示element_count并链接信息就很好了。但是,near_earth_objects为空。我意识到这是因为公共类AsteroidInfo没有在响应中。该节点的名称为2019-07-13。我不知道如何用此信息填充我的AsteroidInfo类。

这是我正在使用的JSON响应的第一部分:

{
    "links": {
        "next": "http://www.neowsapp.com/rest/v1/feed?start_date=2019-07-18&end_date=2019-07-25&detailed=false&api_key=xxxxxxxxxxxxxxxxxxxxxxx",
        "prev": "http://www.neowsapp.com/rest/v1/feed?start_date=2019-07-04&end_date=2019-07-11&detailed=false&api_key=xxxxxxxxxxxxxxxxxxxxxxxx",
        "self": "http://www.neowsapp.com/rest/v1/feed?start_date=2019-07-11&end_date=2019-07-18&detailed=false&api_key=xxxxxxxxxxxxxxxxxxxxxxxx"
    },
    "element_count": 70,
    "near_earth_objects": {
        "2019-07-13": [
            {
                "links": {
                    "self": "http://www.neowsapp.com/rest/v1/neo/3842954?api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                },
                "id": "3842954",
                "neo_reference_id": "3842954",
                "name": "(2019 MW1)",
                "nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3842954",
                "absolute_magnitude_h": 24.505,
                "estimated_diameter": {
                    "kilometers": {
                        "estimated_diameter_min": 0.0333852764,
                        "estimated_diameter_max": 0.0746517476
                    },

我想知道一种在2019-07-13节点下获取信息的方法。

谢谢

1 个答案:

答案 0 :(得分:0)

首先,请注意,如果数据格式不正确,这将非常难以为您提供帮助

第二,NearEarthObjects是某事物的字典

public Dictionary<DateTime , AsteroidInfo[]> near_earth_objects { get; set; }

完整示例

public class Links
{
   public string next { get; set; }
   public string prev { get; set; }
   public string self { get; set; }
}

public class Links2
{
   public string self { get; set; }
}

public class Kilometers
{
   public double estimated_diameter_min { get; set; }
   public double estimated_diameter_max { get; set; }
}

public class EstimatedDiameter
{
   public Kilometers kilometers { get; set; }
}

public class AsteroidInfo
{
   public Links2 links { get; set; }
   public string id { get; set; }
   public string neo_reference_id { get; set; }
   public string name { get; set; }
   public string nasa_jpl_url { get; set; }
   public double absolute_magnitude_h { get; set; }
   public EstimatedDiameter estimated_diameter { get; set; }
}

public class RootObject
{
   public Links links { get; set; }
   public int element_count { get; set; }
   public Dictionary<DateTime , AsteroidInfo[]> near_earth_objects { get; set; }
}

用法

var result = JsonConvert.DeserializeObject<RootObject>(response);

注意:DateTime似乎可以在我的测试中使用,但可能取决于文化