Home > OS >  JsonConvert.DeserializeObject retuns null for a list of objects(in my case satellite positions)
JsonConvert.DeserializeObject retuns null for a list of objects(in my case satellite positions)

Time:07-24

This line is causing my problem. It does load data in the Info but the list gets returned to me as null. How can i fix this?

SatelliteData data = JsonConvert.DeserializeObject<SatelliteData>(con);

enter image description here

That is what the records look like

        public record SatelliteData(Info Info, List<Position> SatellitePositions);
        public record Info
        (
             string Satname,
             decimal Satid,
             decimal Transactionscount
        );
        public record Position(
             decimal Satlatitude,
             decimal Satlongitude,             
             decimal Elevation,
             decimal Sataltitude,
             decimal Azimuth,
             decimal Ra,
             decimal Dec,
             int Timestamp,
             bool Eclipsed
        );

This is what the Json i get from the webAPI(https://www.n2yo.com/api/). I get list of positions but i only need one so i just request one position.

{
  "info": {
    "satname": "SPACE STATION",
    "satid": 25544,
    "transactionscount": 9
  },
  "positions": [
    {
      "satlatitude": -51.62581948,
      "satlongitude": 163.19492627,
      "sataltitude": 436.56,
      "azimuth": 235.45,
      "elevation": -69.02,
      "ra": 31.64722806,
      "dec": -50.69031245,
      "timestamp": 1658524709,
      "eclipsed": false
    }
  ]
}

CodePudding user response:

fix the record, you json property is "positions", but record property "SatellitePositions", should be the same

 public record SatelliteData(Info Info, List<Position> Positions);

or if for some reason you can not change property name

var jsonParsed = JObject.Parse(con);
    
SatelliteData data = new SatelliteData (jsonParsed["info"].ToObject<Info>(),
                            jsonParsed["positions"].ToObject<List<Position>>());

CodePudding user response:

took me about 8 houers of trying diffent stuff out and think the names of the records are what caused the problem? Ether that or the Json PropertyName tags...


            Root root = JsonConvert.DeserializeObject<Root>(con);

    public record Info
    (
        [JsonProperty("satname")] string Satname,
        [JsonProperty("satid")] int Satid,
        [JsonProperty("transactionscount")] int Transactionscount
    );

    public record Position
    (
        [JsonProperty("satlatitude")] double Satlatitude,
        [JsonProperty("satlongitude")] double Satlongitude,
        [JsonProperty("sataltitude")] double Sataltitude,
        [JsonProperty("azimuth")] double Azimuth,
        [JsonProperty("elevation")] double Elevation,
        [JsonProperty("ra")] double Ra,
        [JsonProperty("dec")] double Dec,
        [JsonProperty("timestamp")] int Timestamp,
        [JsonProperty("eclipsed")] bool Eclipsed
    );

    public record Root
    (
        [JsonProperty("info")] Info Info,
        [JsonProperty("positions")] List<Position> Positions
    );
  • Related