【问题标题】:Bing Maps REST services JSON deserialziation error必应地图 REST 服务 JSON 反序列化错误
【发布时间】:2014-07-24 15:44:07
【问题描述】:

我正在尝试通过 Bing 地图 REST 服务获取行车路线。响应似乎正在通过,但是当使用提供的数据协定反序列化为 JSON 时,会引发以下异常:

元素“:item”包含映射到名称“http://schemas.microsoft.com/search/local/ws/rest/v1:Route”的类型的数据。反序列化器不知道映射到此名称的任何类型。考虑使用 DataContractResolver 或将与 'Route' 对应的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将其添加到传递给 DataContractSerializer 的已知类型列表中。

我正在使用的 MSDN 链接位于:http://msdn.microsoft.com/en-us/library/jj819168.aspx

我已从示例底部的链接中复制了必要的 DataContract,但无法弄清楚反序列化时发生异常的原因。这是我正在执行的代码:

private async void btnGetRoute_Click(object sender, RoutedEventArgs e)
    {
            string locationFrom = "100 Commonwealth Ave Boston MA";
            string locationTo = "100 Yawkey Way Boston MA";
            string url = string.Format("http://dev.virtualearth.net/REST/V1/Routes/Driving?wp.0={0}&wp.1={1}&key={2}", locationFrom, locationTo, ConfigurationManager.AppSettings["BingMapsKey"]);
            Uri drivingRouteRequest = new Uri(url);

            Response response = await GetResponse(drivingRouteRequest);
    }

    private async Task<Response> GetResponse(Uri uri)
    {
        HttpClient client = new System.Net.Http.HttpClient();
        var response = await client.GetAsync(uri);

        using (var stream = await response.Content.ReadAsStreamAsync())
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response));
            return ser.ReadObject(stream) as Response;
        }
    }

有什么想法吗?

【问题讨论】:

  • 这方面有什么进展吗?我注意到了同样的问题 [这里][1]。汤姆 [1]:stackoverflow.com/questions/25297238/…
  • @Tom no... 我们使用 Telerik。相反,我能够通过它们获得行车路线。我没有再尝试过这种方法,所以现在无法提供任何进一步的信息。

标签: c# json rest bing-maps bing-api


【解决方案1】:

Bing Maps 团队已经意识到这一点,并且即将更新文档。问题是正在返回一个新对象,而以前我们只知道属性名称。我的一个项目中有一组更新的数据合约,您可以在这里找到:http://mapstoolbox.codeplex.com/SourceControl/latest#Microsoft.Maps.Spatialtoolbox/Source/Microsoft.Maps.SpatialToolbox.Core/Bing/Services/RestServices.cs

【讨论】:

  • 是否可以通过 MSDN 网站某处的链接获得更新文档的信息,或者您是否从私人支持票证中获得信息?
  • 我在 Bing 地图团队工作,根据我在服务响应中看到的内容创建了数据合同文档。我不在工程团队,也没有创建服务,我必须通过反复试验找出数据合同。
  • 明白了。我重试了来自该链接中数据合同的 JSON 请求,并得到了相同的结果。我能够通过 XML 成功完成请求,但是当尝试从 ResourceSets[0].Resources[0] 分配路由时,Resources[0] 始终为空。我可以在 XML 输出中看到路线的所有详细信息。
【解决方案2】:

This thread at MSDN 确认,来自 Microsoft 的原始类不起作用。但是,如果您只是使用 json2csharp.com 生成自己的类并使用这些类进行反序列化,它就可以工作。

汤姆

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多