Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with deserializing JSON on datamember "__type"
    primarykey
    data
    text
    <p>In short, i'm trying to deserialize a JSON response from the Bing Maps Geocoding REST API,</p> <p>I created my Response Class, and now when I'm trying to actually deserialize a response, i'm getting the following error:</p> <p>Type '{0}' with data contract name '{1}:{2}' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.</p> <p>it's trying to deserialize this line of JSON, and fails:</p> <pre><code>"__type": "Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1", </code></pre> <p>My response class looks like this</p> <pre><code> [DataContract] public class GeoResponse { [DataMember(Name = "statusDescription")] public string StatusDescription { get; set; } [DataMember(Name = "statusCode")] public string StatusCode { get; set; } [DataMember(Name = "resourceSets")] public ResourceSet[] resourceSets { get; set; } [DataContract] public class ResourceSet { [DataMember(Name = "__type", IsRequired=false)] public string type { get; set; } [DataMember(Name = "estimatedTotal")] public string EstimatedTotal { get; set; } [DataMember(Name = "resources")] public List&lt;Resources&gt; resources { get; set; } [DataContract] public class Resources { [DataMember(Name = "name")] public string Name { get; set; } [DataMember(Name = "point")] public Point point { get; set; } [DataContract] public class Point { [DataMember(Name = "type")] public string Type { get; set; } [DataMember(Name = "coordinates")] public string[] Coordinates { get; set; } } [DataMember(Name = "address")] public Address address { get; set; } [DataContract] public class Address { [DataMember(Name = "addressLine")] public string AddressLine { get; set; } [DataMember(Name = "countryRegion")] public string CountryRegion { get; set; } [DataMember(Name = "formattedAddress")] public string FormattedAddress { get; set; } [DataMember(Name = "locality")] public string Locality { get; set; } [DataMember(Name = "postalCode")] public string PostalCode { get; set; } } [DataMember(Name = "confidence")] public string Confidence { get; set; } [DataMember(Name = "entityType")] public string EntityType { get; set; } } } } } </code></pre> <p>My method i'm using to deserialize my JSON response:</p> <pre><code>private static GeoResponse CallGeoWS(string address) { string url = string.Format( "http://dev.virtualearth.net/REST/v1/Locations?q={0}&amp;key={1}", HttpUtility.UrlEncode(address), bingkey ); var request = (HttpWebRequest)HttpWebRequest.Create(url); request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate"); request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(GeoResponse)); var res = (GeoResponse)serializer.ReadObject(request.GetResponse().GetResponseStream()); return res; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload