2

I already looked at a lot of other questions with the same problem but never found a definitive solution that actually works for me. I tried using the JsonExtensionData Attribute, that doesn't work though since I can't convert my other data class to an object and it throws the Invalid extension data attribute on 'NAMESPACE'. Member 'Sols' type must implement IDictionary<string, JToken>. error.

My current data model class looks like this

public partial class Mars
{
    public Dictionary<string, Sol> Sols { get; set; }

    [JsonProperty("sol_keys")]
    public List<long> SolKeys { get; set; }
}

public partial class Sol
{
    [JsonProperty("AT")]
    public At At { get; set; }

    [JsonProperty("First_UTC")]
    public DateTimeOffset FirstUtc { get; set; }

    [JsonProperty("Last_UTC")]
    public DateTimeOffset LastUtc { get; set; }

    [JsonProperty("Season")]
    public string Season { get; set; }
}

public partial class At
{
    [JsonProperty("av")]
    public double Av { get; set; }

    [JsonProperty("ct")]
    public long Ct { get; set; }

    [JsonProperty("mn")]
    public double Mn { get; set; }

    [JsonProperty("mx")]
    public double Mx { get; set; }
}

The json data looks like this

{
    "651": 
    {
        "AT": 
        {
            "av": -61.957,
            "ct": 302204,
            "mn": -96.733,
            "mx": -15.877
        },
        "First_UTC": "2020-09-25T02:42:14Z",
        "Last_UTC": "2020-09-26T03:21:49Z",
        "Season": "summer"
    },
    "652": {
        "AT": {
            "av": -65.002,
            "ct": 278608,
            "mn": -96.111,
            "mx": -15.653
        },
        "First_UTC": "2020-09-26T03:21:50Z",
        "Last_UTC": "2020-09-27T04:01:24Z",
        "Season": "summer"
    },
    "sol_keys": [
        "646",
        "647",
        "648",
        "649",
        "650",
        "651",
        "652"
    ]
}

I can't really modify the json data since I get it from an api. I basically just want to select one of the numbers and then get the Sol data of that object.

Any help would be appreciated.

1 Answer 1

2

The JSON doesn't fit well with the C# type system. However, you can still use Json.Net to parse it. You just need to introduce some extra steps.

First step is to parse the JSON to a JObject:

var jObject = JsonConvert.DeserializeObject<JObject>(json);

Then you can extract the sol_keys:

var solKeys = jObject.GetValue("sol_keys").ToObject<long[]>();

Now it becomes a bit tricky. If you remove the sol_keys from the JSON (in this case the parsed JSON) it has the structure of a dictionary of Sol objects that you are able to parse:

jObject.Remove("sol_keys");
var mars = jObject.ToObject<Dictionary<long, Sol>>();

Now you have both solKeys and mars parsed from the JSON. Furthermore the solKeys and the keys in the dictionary share the same type (long).

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I asked for. And yeah, the json provided by the api is pretty weird. If you're wondering its from the official Nasa api

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.