-1

I am trying to parse a Json response in C#, but one of the parent nodes is a date like "2019-07-11." Normally I would just make classes to deserialize my response into, but this date node is throwing me for a loop.

Here is what I am doing. I am getting a JSON response and trying to deserialize it into nice classes:

   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);

Here are my classes:

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; }
   }

In my RootAsteroidOjbect object after parsing it shows the element_count and links info just fine. However, the near_earth_objects is null. I realize this is because public class AsteroidInfo isn't in the response. The node has the name 2019-07-13. I don't know how to populate my AsteroidInfo class with this information.

Here is the first part of the JSON response I'm working with:

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

I would like to know an approach to get the info under the 2019-07-13 node.

Thanks

TheOracle
  • 9
  • 1

1 Answers1

0

Firstly, please note it makes it extremely difficult to help you when you have malformed data

Secondly, NearEarthObjects is a dictionary of something

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

Full Example

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; }
}

Usage

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

Note : The DateTime seems to work with my testing, however it maybe culture dependent

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • I cannot see the asteroid property in the JSON - maybe I am blind – Sir Rufo Jul 11 '19 at 23:59
  • @SirRufo good point, i should put more effort in :) – TheGeneral Jul 12 '19 at 00:00
  • I am not sure if you can deserialize JSON to a Dictionary but I never tested it – Sir Rufo Jul 12 '19 at 00:18
  • 1
    @SirRufo i was a bit unsure of that my self, seems to work though, ie try parse seems to recognise that date format, but it maybe culture dependent, i should make a note – TheGeneral Jul 12 '19 at 00:18
  • 1
    Even you told me I had to [proof myself](https://dotnetfiddle.net/xYtFcg) :o) - I would expect the property deserialization will use the default JSON datetime handling (ISO8601) so it will not depend on culture - but that is just a guess and not a proof – Sir Rufo Jul 12 '19 at 06:41
  • 1
    @SirRufo ahh... good old *ISO8601*.. **cough** actually i just learnt about it, seems legit though. yeah though i'm not sure what *json.net* does under the hood, it seems to work though, i wouldn't send rockets to the moon on the premise though. – TheGeneral Jul 12 '19 at 06:46
  • Thank you very much @TheGeneral Works perfectly for me! – TheOracle Jul 15 '19 at 22:47