2

I have a response from the server:

"results": {
        "231412354": [
            {
              "id": "11913258",
              "name" : "dsa"
            }
        ],
        "546547654": [
            {
              "id": "11913258",
              "name" : "dsa"
            },
        ],
}

My model for deserializing

public partial class Welcome
{
    public Dictionary<long, Result[]> Results { get; set; }
}

public partial class Result
{
    public long Id { get; set; }
    public string Name { get; set; }
}

But sometimes the server can return an empty result that will look like an array

"results": []

And i get an deserializing error, I tried to add null value handling and not required, maybe need to add a custom converter. Could I have help in this matter?

  • 1
    I think `JsonSingleOrEmptyArrayConverter>` from [this answer](https://stackoverflow.com/a/29450279/3744182) to [Deserialize JSON when type can be different](https://stackoverflow.com/q/29449641/3744182) should work for you. – dbc Aug 01 '19 at 07:18
  • Can you show us the code of your deserialization? – taktak Aug 01 '19 at 07:18
  • Did you check this out : [stackoverflow answer](https://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net) – taktak Aug 01 '19 at 07:20
  • 1
    Demo fiddle of the answer from [this answer](https://stackoverflow.com/a/29450279/3744182) to [Deserialize JSON when type can be different](https://stackoverflow.com/q/29449641/3744182) here: https://dotnetfiddle.net/GDkkfn. Another example is [Cannot Deserialize the Current JSON Object (Empty Array)](https://stackoverflow.com/q/53219668/3744182). – dbc Aug 01 '19 at 07:26
  • @dbc You should post this as an answer, not a comment ;) – taktak Aug 01 '19 at 07:28
  • Well I'd really just suggest marking it as a duplicate as long as the answer works for you. – dbc Aug 01 '19 at 07:29
  • I agree. Most of the part of your answer is dealing with another stackoverflow post – taktak Aug 01 '19 at 07:30
  • 1
    @swdon - I'm almost certain that the only thing missing are the `{` and `}` at the beginning and the end. Add them in and the JSON is well-formed. – dbc Aug 01 '19 at 07:42

0 Answers0