-1

I am trying to deserialize below json with the first value equal to null, into simple array with doubles. However I am getting error The best overloaded method match for 'Newtonsoft.Json.JsonConvert.DeserializeObject<double[]>(string, params Newtonsoft.Json.JsonConverter[])' has some invalid arguments

{[
  null,
  0.1,
  0.2,
  0.3
]}

The code is

var values = JsonConvert.DeserializeObject<double[]>(valJson, new JsonSerializerSettings
{
   NullValueHandling = NullValueHandling.Ignore,
   MissingMemberHandling = MissingMemberHandling.Ignore
});

I have tried multiple configurations i.e. NullValueHandling.Include without any success. What's wrong with the code?

Serge
  • 40,935
  • 4
  • 18
  • 45
Jim
  • 2,760
  • 8
  • 42
  • 66
  • 1
    1) 2) Why not just deserialize to `List` and filter afterwards? 2) Your JSON is malformed. It should not have the outer braces `{ }`. That might just be due to Visual Studio "helpfully" visualizing the JSON for you, so can you confirm the braces are not present by checking the raw JSON? – dbc Dec 14 '21 at 18:52
  • 1
    By the way, I tried to reproduce your problem but I am not getting the compilation error you are seeing. Instead I am getting a runtime error *`Newtonsoft.Json.JsonSerializationException: Error converting value {null} to type 'System.Double'. Path '[0]', line 2, position 6.`*. See https://dotnetfiddle.net/8WJn2d. Is `valJson` really a string, or is it a `JArray`? – dbc Dec 14 '21 at 18:56
  • 1
    Demo of filtering here: https://dotnetfiddle.net/6SIDvc. Is that all you want? If not, see [Json.Net: How to ignore null elements in array deserializing a JSON](https://stackoverflow.com/q/62940751/3744182), of which your question may be a duplicate; agree? – dbc Dec 14 '21 at 18:58

1 Answers1

2

just instead of double use double?, It was tested in Visual Studio, no any problem at all, everything is working properly.

double?[] d = JsonConvert.DeserializeObject<double?[]>(json);
Serge
  • 40,935
  • 4
  • 18
  • 45