I am working with the Twitter API, accessing it via C# and DataContracts.
I have trouble reading in the geo field. The problem is that sometimes it contains sub fields, while sometimes it is null:
"geo":{"coordinates":[52.5112,13.3577],"type":"Point"}
or
"geo":null
I am trying it with
[DataContract]
public class Geo
{
[DataMember(Name = "coordinates")]
public string Coordinates { get; set; }
}
The error I get is:
"There was an error deserializing the object of type Twitter.SearchResults. End element 'coordinates' from namespace '' expected. Found element 'item' from namespace ''."
Seems like the parser hit a null, but expected a coordinates field. I understand that I told the parser to expect "coordinates", so of course it complains. But I do not know how I can make this field optional.
If I'm not mistaken, all properties/fields are optional by default. The problem rather seems to be that coordinates isn't a string property, but an array of floating point numbers.
So to fix it, try:
[DataContract]
public class Geo
{
[DataMember(Name = "coordinates")]
public double[] Coordinates { get; set; }
}