I'm trying to deserialize some Json:
{
"name": "foo",
"value": [ [ 1.2, 2.3, 4.5 ], [ 1.2, 2.3, 4.5 ] ]
}
into this C# class:
class Bar {
public string name { get; set; }
public object value { get; set; }
}
value is of type object
because it can be a single value or any array of array, of..., of values.
I've tried with the native C# class:
string jsonString = @"{
""name"": ""foo"",
""value"": [ [ 1.2, 2.3, 4.5 ], [ 1.2, 2.3, 4.5 ] ]
}";
var data1 = new JavaScriptSerializer().Deserialize<Bar>(jsonString).value;
data1
is an object[]
of object[]
that are in fact decimal
. Problem is: I need them to be doubles
.
So I've tried with the Json.NET library:
var data2 = JsonConvert.DeserializeObject<Bar>(
jsonString,
new JsonSerializerSettings { FloatParseHandling = FloatParseHandling.Double }
).value;
Now the final values are of type double
but I lost the structure of arrays of objects, and have instead a JArray
of JArray
of double
.
So my question is: Is it possible to configure the native JavaScriptSerializer
class to get doubles
instead of decimals
or is it possible to make Json.NET return arrays of objects
?