Reputation: 1339
I'm working with an API that returns JSON like this (simplified to relevant props):
{"coordinates":[[[-119.99267578125,38.9594087924542],[-120.08056640625,38.9764924855394],[-120.146484375,39.0789080970648],[-120.08056640625,39.2152313091049],[-119.94873046875,39.2152313091049],[-119.99267578125,38.9594087924542]]]}
You'll notice that these numbers have greater precision than a float. I've created the following class to hold this:
public class JsonParseTest
{
public List<List<decimal[,]>> coordinates { get; set; }
}
And I'm using this code to parse:
try
{
var json = "{\"coordinates\":[[[-119.99267578125,38.9594087924542],[-120.08056640625,38.9764924855394],[-120.146484375,39.0789080970648],[-120.08056640625,39.2152313091049],[-119.94873046875,39.2152313091049],[-119.99267578125,38.9594087924542]]]}";
var obj = JsonConvert.DeserializeObject<JsonParseTest>(json, new JsonSerializerSettings()
{
FloatParseHandling = FloatParseHandling.Decimal
});
}
catch (Exception ex)
{
}
The exception is:
Unexpected token when deserializing multidimensional array: Float. Path 'coordinates[0][0][0]', line 1, position 34.
Am I doing something wrong? As far as I can tell, this appears to be an error with Newtonsoft.
Upvotes: 2
Views: 218
Reputation: 116785
Your JsonParseTest
should look like:
public class JsonParseTest
{
public List<decimal[,]> coordinates { get; set; }
}
I.e. remove one level of List<>
. Your current class corresponds to four levels of bracket nesting (one for each List<>
, two for the decimal [,]
) but your JSON only has three levels of nesting.
To discover this yourself, you could have serialized an example of your class out to JSON. If you had done so, you would have seen the extra nesting.
Upvotes: 2