none
Resoucesets RRS feed

  • Question

  • I'm doing a REST call to Bing-Driving Api:

    http://dev.virtualearth.net/REST/V1/Routes/Driving?o=json&wp.1=-22.57041,17.11387&wp.2=-22.57623,17.06172&key=...

    After de-serialising the response:

    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response));
    r = ser.ReadObject(stream) as Response;

    I'm supposed to get the Route-object with:

    var route = (r.ResourceSets[0].Resources[0] as Route)

    But in my case r.ResourceSets[0].Resources[0] is only a BoundingBox-object! Casting to Route gives null. How do I get the Route? I can see in the raw-JSON, that all route-data is there.

    The JSON data contract from here is added to my project:

    http://msdn.microsoft.com/en-us/library/jj870778.aspx

    Thanks Tom


    Thursday, August 14, 2014 1:09 PM

Answers

  • Hi Tom,

    You are using the Bing Maps services correctly. Testing it from my side id does work correctly with the JSON contracts. If you are looking at the ResourceSet object in visual studios it might look like their is just a boundingbox, however if you look at the base object it is actually a Route object. Here is the code I'm using to case the route.

    if (r != null &&
    	r.ResourceSets != null &&
    	r.ResourceSets.Length > 0 &&
    	r.ResourceSets[0].Resources != null &&
    	r.ResourceSets[0].Resources.Length > 0)
    {
    	Route route = r.ResourceSets[0].Resources[0] as Route;	
    }


    http://rbrundritt.wordpress.com

    Friday, August 15, 2014 10:07 AM

All replies

  • Thanks Mudassir,

    I didn't know about json2csharp.com, very helpful. And actually a shame that the original classes from Microsoft are not working.

    Tom

    Friday, August 15, 2014 9:58 AM
  • Hi Tom,

    You are using the Bing Maps services correctly. Testing it from my side id does work correctly with the JSON contracts. If you are looking at the ResourceSet object in visual studios it might look like their is just a boundingbox, however if you look at the base object it is actually a Route object. Here is the code I'm using to case the route.

    if (r != null &&
    	r.ResourceSets != null &&
    	r.ResourceSets.Length > 0 &&
    	r.ResourceSets[0].Resources != null &&
    	r.ResourceSets[0].Resources.Length > 0)
    {
    	Route route = r.ResourceSets[0].Resources[0] as Route;	
    }


    http://rbrundritt.wordpress.com

    Friday, August 15, 2014 10:07 AM
  • Ricky,

    I double-checked the whole story now, even took the datacontract freshly from http://msdn.microsoft.com/en-us/library/jj870778.aspx.

    Your proposal, which is supposed to be the official way, is not working for me. It just gives null.

    The classes generated from JSON2csharp are slightly different. Of course, some properties are missing, but it was easy to add the few missing things I needed.

    The main difference is, that you guys are using arrays and JSON2csharp is generating lists. Maybe thats the problem. And I'm actually fed-up, that these simple things are costing me now already 1.5 days and only Syed helped me out of that. Thanks Syed agin.

    Tom

    Friday, August 15, 2014 10:53 AM
  • Hi Ricky,

    I am having the same problem as Tom.  Using the data contracts provided at https://msdn.microsoft.com/en-us/library/jj870778.aspx the 'Resource' object is not deserializing correctly.  The 'BoundingBox' property is the only property available.  This is not an issue of how it is being represented in the Visual Studio debugger.  There is no 'base' object to look at.  Attempting to cast the object to a 'Location' object (in my case) using the 'as' keyword simply results in null.  I am using Json.Net 6.0.3 for deserialization.  

    What is interesting is that I have nearly identical data contracts that I used in a different project early last year.  They came from the same site but in these data contracts the 'Response' and 'ResourceSet' objects are generic types.  I can't remember if I downloaded them like that or if I added the generic type to them.

    Thursday, March 19, 2015 4:05 PM
  • These are Data Contracts, which should be deserialized using the DataContractJsonSerializer which is in the System.Runtime.Serialization library of .NET, no 3rd party library needed.

    http://rbrundritt.wordpress.com

    Friday, March 20, 2015 4:54 PM