1

I'm trying to download json data where one of the attributes is "easting". For the majority of the json dataset, "easting" is an integer, but there are a handful of cases in which "easting" is a list of integers as shown below:

"easting" : 357570 ,
...
"easting" : [ 434939, 434942 ] ,

My original code had anticipated "easting" to be an int when its class was declared as follows:

public class Item
{
    public int easting { get; set; }
}

However, I keep getting errors saying that I cannot convert a type int to a type int []. If I adjust the class definition to allow easting to be of type int [], I simply get the reverse error - I cannot convert a type int [] to a type int.

How can I go about handling this? Is there any way to account for a varying input datatype during the deserialization process?

jcusick
  • 50
  • 7
  • Can you use a dynamic? Check this out: http://stackoverflow.com/questions/30147272/serialize-a-json-property-that-is-sometimes-an-array – jpgrassi Sep 01 '16 at 20:01
  • 1
    If you're using Json.net, then this is a duplicate of [this question](http://stackoverflow.com/questions/18994685/how-to-handle-both-a-single-item-and-an-array-for-the-same-property-using-json-n) – stuartd Sep 01 '16 at 20:31
  • @stuartd I'm currently using the `JavaScriptSerializer` from `System.Web.Script.Serialization` though I might try switching to Json.net as that example does seem straightforward. Thanks. – jcusick Sep 01 '16 at 20:37

1 Answers1

0

In the end, I followed the suggestion from @stuartd and swtiched from JavaScriptSerializer to Json.net for the deserialization process, which allowed me to follow along with the question posted here and resolve my issue.

Community
  • 1
  • 1
jcusick
  • 50
  • 7