Bing-Rest-Api Get Route from Response.ResourceSets

2019-09-17 06:35发布

问题:

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! How do I get the Route? I can see in the raw-JSON, that everything is there.

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

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

Thanks Tom

回答1:

Tom,

I was able to get those same data contracts (https://msdn.microsoft.com/en-us/library/jj870778.aspx) working in my project by making a few minor changes to the data contracts after I copied them into my solution. I should note that I am using Json.Net 6.0.3 for deserialization and not the DataContractJsonSerializer. I do not know if that makes a difference.

First I changed this:

public class Response
{
    [DataMember(Name = "copyright", EmitDefaultValue = false)]
    public string Copyright { get; set; }

    [DataMember(Name = "brandLogoUri", EmitDefaultValue = false)]
    public string BrandLogoUri { get; set; }

    [DataMember(Name = "statusCode", EmitDefaultValue = false)]
    public int StatusCode { get; set; }

    [DataMember(Name = "statusDescription", EmitDefaultValue = false)]
    public string StatusDescription { get; set; }

    [DataMember(Name = "authenticationResultCode", EmitDefaultValue = false)]
    public string AuthenticationResultCode { get; set; }

    [DataMember(Name = "errorDetails", EmitDefaultValue = false)]
    public string[] errorDetails { get; set; }

    [DataMember(Name = "traceId", EmitDefaultValue = false)]
    public string TraceId { get; set; }

    [DataMember(Name = "resourceSets", EmitDefaultValue = false)]
    public ResourceSet[] ResourceSets { get; set; }
}

to this (the only change was the addition of the generic type to the class and the ResourceSet property):

public class Response<T>
{
    [DataMember(Name = "copyright", EmitDefaultValue = false)]
    public string Copyright { get; set; }

    [DataMember(Name = "brandLogoUri", EmitDefaultValue = false)]
    public string BrandLogoUri { get; set; }

    [DataMember(Name = "statusCode", EmitDefaultValue = false)]
    public int StatusCode { get; set; }

    [DataMember(Name = "statusDescription", EmitDefaultValue = false)]
    public string StatusDescription { get; set; }

    [DataMember(Name = "authenticationResultCode", EmitDefaultValue = false)]
    public string AuthenticationResultCode { get; set; }

    [DataMember(Name = "errorDetails", EmitDefaultValue = false)]
    public string[] errorDetails { get; set; }

    [DataMember(Name = "traceId", EmitDefaultValue = false)]
    public string TraceId { get; set; }

    [DataMember(Name = "resourceSets", EmitDefaultValue = false)]
    public ResourceSet<T>[] ResourceSets { get; set; }
}

Then I changed this:

public class ResourceSet
{
    [DataMember(Name = "estimatedTotal", EmitDefaultValue = false)]
    public long EstimatedTotal { get; set; }

    [DataMember(Name = "resources", EmitDefaultValue = false)]
    public Resource[] Resources { get; set; }
}

to this:

public class ResourceSet<T>
{
    [DataMember(Name = "estimatedTotal", EmitDefaultValue = false)]
    public long EstimatedTotal { get; set; }

    [DataMember(Name = "resources", EmitDefaultValue = false)]
    public T[] Resources { get; set; }
}

and everything worked.