80569

Metro Style App: JSON Object Deserialization Error, REST Services

Question:

<blockquote>

Element ':item' contains data from a type that maps to the name "http://schemas.microsoft.com/search/local/ws/rest/v1:Route.' The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'Route' to the list of known types - for example, by using KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.

</blockquote> <hr />

After adding [DataContract(Namespace = "http://schemas.microsoft.com/search/local/ws/rest/v1", Name = "Location")] ro Resource class, I got this exception:

Expecting state 'Element'.. Encountered 'Text' with name '', namespace ''

<hr />

Likn: <a href="http://dev.virtualearth.net/REST/V1/Routes?wp.0=37.779160067439079,-122.42004945874214&wp.1=32.715685218572617,-117.16172486543655&key=BingMapsKey" rel="nofollow">http://dev.virtualearth.net/REST/V1/Routes?wp.0=37.779160067439079,-122.42004945874214&wp.1=32.715685218572617,-117.16172486543655&key=BingMapsKey</a>

[KnownType(typeof(double[]))] [DataContract] public class ActualEnd { [DataMember(Name = "type")] public string type { get; set; } [DataMember(Name = "coordinates")] public List<double> coordinates { get; set; } } [KnownType(typeof(double[]))] [DataContract] public class ActualStart { [DataMember(Name = "type")] public string type { get; set; } [DataMember(Name = "coordinates")] public List<double> coordinates { get; set; } } [KnownType(typeof(int[]))] [KnownType(typeof(string[]))] [DataContract] public class Detail { [DataMember(Name = "compassDegrees")] public int compassDegrees { get; set; } [DataMember(Name = "endPathIndices")] public List<int> endPathIndices { get; set; } [DataMember(Name = "maneuverType")] public string maneuverType { get; set; } [DataMember(Name = "mode")] public string mode { get; set; } [DataMember(Name = "roadType")] public string roadType { get; set; } [DataMember(Name = "startPathIndices")] public List<int> startPathIndices { get; set; } [DataMember(Name = "names")] public List<string> names { get; set; } } [DataContract] public class Instruction { [DataMember(Name = "maneuverType")] public string maneuverType { get; set; } [DataMember(Name = "text")] public string text { get; set; } } [KnownType(typeof(double[]))] [DataContract] public class ManeuverPoint { [DataMember(Name = "type")] public string type { get; set; } [DataMember(Name = "coordinates")] public List<double> coordinates { get; set; } } [DataContract] public class Hint { [DataMember(Name = "hintType")] public object hintType { get; set; } [DataMember(Name = "text")] public string text { get; set; } } [KnownType(typeof(Detail[]))] [KnownType(typeof(Instruction))] [KnownType(typeof(Hint[]))] [KnownType(typeof(ManeuverPoint))] [KnownType(typeof(string[]))] [DataContract] public class ItineraryItem { [DataMember(Name = "compassDirection")] public string compassDirection { get; set; } [DataMember(Name = "details")] public List<Detail> details { get; set; } [DataMember(Name = "exit")] public string exit { get; set; } [DataMember(Name = "iconType")] public string iconType { get; set; } [DataMember(Name = "instruction")] public Instruction instruction { get; set; } [DataMember(Name = "maneuverPoint")] public ManeuverPoint maneuverPoint { get; set; } [DataMember(Name = "sideOfStreet")] public string sideOfStreet { get; set; } [DataMember(Name = "tollZone")] public string tollZone { get; set; } [DataMember(Name = "towardsRoadName")] public string towardsRoadName { get; set; } [DataMember(Name = "transitTerminus")] public string transitTerminus { get; set; } [DataMember(Name = "travelDistance")] public double travelDistance { get; set; } [DataMember(Name = "travelDuration")] public int travelDuration { get; set; } [DataMember(Name = "travelMode")] public string travelMode { get; set; } [DataMember(Name = "signs")] public List<string> signs { get; set; } [DataMember(Name = "hints")] public List<Hint> hints { get; set; } } [KnownType(typeof(ActualEnd))] [KnownType(typeof(ActualStart))] [KnownType(typeof(ItineraryItem[]))] [DataContract] public class RouteLeg { [DataMember(Name = "actualEnd")] public ActualEnd actualEnd { get; set; } [DataMember(Name = "actualStart")] public ActualStart actualStart { get; set; } [DataMember(Name = "itineraryItems")] public List<ItineraryItem> itineraryItems { get; set; } [DataMember(Name = "travelDistance")] public double travelDistance { get; set; } [DataMember(Name = "travelDuration")] public int travelDuration { get; set; } } [KnownType(typeof(RouteLeg[]))] [KnownType(typeof(double[]))] [DataContract(Namespace = "http://schemas.microsoft.com/search/local/ws/rest/v1", Name = "Location")] public class Resource { [DataMember(Name = "__type")] public string __type { get; set; } [DataMember(Name = "id")] public List<double> bbox { get; set; } public string id { get; set; } [DataMember(Name = "distanceUnit")] public string distanceUnit { get; set; } [DataMember(Name = "durationUnit")] public string durationUnit { get; set; } [DataMember(Name = "routeLegs")] public List<RouteLeg> routeLegs { get; set; } [DataMember(Name = "travelDistance")] public double travelDistance { get; set; } [DataMember(Name = "travelDuration")] public int travelDuration { get; set; } } [KnownType(typeof(Resource[]))] [DataContract] public class ResourceSet { [DataMember(Name = "estimatedTotal")] public int estimatedTotal { get; set; } [DataMember(Name = "resources")] public List<Resource> resources { get; set; } } [KnownType(typeof(ResourceSet[]))] [DataContract] public class RootObject { [DataMember(Name = "authenticationResultCode")] public string authenticationResultCode { get; set; } [DataMember(Name = "brandLogoUri")] public string brandLogoUri { get; set; } [DataMember(Name = "copyright")] public string copyright { get; set; } [DataMember(Name = "resourceSets")] public List<ResourceSet> resourceSets { get; set; } [DataMember(Name = "statusCode")] public int statusCode { get; set; } [DataMember(Name = "statusDescription")] public string statusDescription { get; set; } [DataMember(Name = "traceId")] public string traceId { get; set; } } public async void MakeRequest(string requestUrl) { try { HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest; using (HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse) { if (response.StatusCode != HttpStatusCode.OK) throw new Exception(string.Format( "Server error(HTTP {0}:{1}.", response.StatusCode, response.StatusDescription)); DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(RootObject)); object objResponse = jsonSerializer.ReadObject(response.GetResponseStream()); root = (RootObject)objResponse; } } catch (Exception ex) { ThrowException(ex); } }

The problem is here:

"estimatedTotal": 1, "resources": [ { "__type": "Route:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1", "bbox": [ 32.715692, -122.420697, 37.827532, -117.161052 ],

Answer1:

Take a look at this blog post: <a href="http://rbrundritt.wordpress.com/2012/01/06/bing-maps-rest-service-net-libraries/" rel="nofollow">http://rbrundritt.wordpress.com/2012/01/06/bing-maps-rest-service-net-libraries/</a>

It has all the libraries you need for serializing the REST services. For the error you are seeing you need to create a class called Route and mark it as a known type on the resource class.

For Example:

[DataContract] [KnownType(typeof(Location))] [KnownType(typeof(Route))] public class Resource

Recommend

  • Controlling WCF Message Body serialization
  • C# How to handle polymorphic models using NSwag
  • Unable to load related entities in a WCF service
  • How to write string.Contains(someText) in expression Tree
  • DependencyObject.AssociatedObject is always null
  • Receive list of elements in their visual order
  • when does setTimeout start executing in a inline
  • How do I register classes by both interface and namespace with Windsor?
  • gspread or such: help me get cell coordinates (not value)
  • Use of this Javascript
  • When to use `image` and when to use `Matrix` in Emgu CV?
  • R - Combining Columns to String Based on Logical Match
  • Linq Objects Group By & Sum
  • How do I change content of ComboFieldEditor?
  • Counter field in MS Access, how to generate?
  • Is there a amazon webstore API for customers?
  • Can Jackson SerializationFeature be overridden per field or class?
  • How to extract text from Word files using C#?
  • vba code to select only visible cells in specific column except heading
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • How to format a variable of double type
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • Buffer size for converting unsigned long to string
  • KeystoneJS: Relationships in Admin UI not updating
  • trying to dynamically update Highchart column chart but series undefined
  • JTable with a ScrollPane misbehaving
  • embed rChart in Markdown
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • apache spark aggregate function using min value
  • unknown Exception android
  • Running Map reduces the dimensions of the matrices
  • Sorting a 2D array using the second column C++
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • reshape alternating columns in less time and using less memory
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?