元素“:项目”包含来自映射到名称“http://...:Location”的类型的数据。解串器不知道映射到此的任何类型

发布于 2024-11-19 19:27:00 字数 2396 浏览 0 评论 0原文

我正在编写代码来使用 Bing 地理编码服务(Bing 地图),并且我成功地通过 wcf 提取 JSON 数据,不幸的是我似乎无法映射返回的数据。

我根据需要创建了所有适当的 DataContracts 并用所需的成员填充它们,但是当我开始进入子项时,我收到以下错误:

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

因此,我注释掉了“子”对象,并且基本上能够辨别出当它尝试读取

下面代码中 JSON 对象的“位置”部分时它正在爆炸,它涉及这里的部分:

               "__type":"Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",

其价值是什么,网址很糟糕,但我不在乎。我不想使用该类型(它显然映射回 Microsoft 网站上的架构)。有没有办法告诉 WCF 忽略该链接? 做不到。

Bing 返回的内容

{
   "authenticationResultCode":"ValidCredentials",
   "brandLogoUri":"http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png",
   "copyright":"Copyright © 2010 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.",
   "resourceSets":[
      {
         "estimatedTotal":1,
         "resources":[
            {
               "__type":"Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",
               "bbox":[
                  47.635884282429323,
                  -122.13737419709076,
                  47.643609717570676,
                  -122.12208780290925
               ],
               "name":"1 Microsoft Way, Redmond, WA 98052-8300",
               "point":{
                  "type":"Point",
                  "coordinates":[
                     47.639747,
                     -122.129731
                  ]
               },
               "address":{
                  "addressLine":"1 Microsoft Way",
                  "adminDistrict":"WA",
                  "adminDistrict2":"King County",
                  "countryRegion":"United States",
                  "formattedAddress":"1 Microsoft Way, Redmond, WA 98052-8300",
                  "locality":"Redmond",
                  "postalCode":"98052-8300"
               },
               "confidence":"High",
               "entityType":"Address"
            }
         ]
      }
   ],
   "statusCode":200,
   "statusDescription":"OK",
   "traceId":"43c6a4dc130749bbb14eb72bf12c4198 "
}

I'm writing code to consume the Bing Geocode service (Bing Maps), and I am successfully able to pull JSON data via wcf, unfortunately I seem to be unable to map a piece of the data returned.

I created all the appropriate DataContracts as needed and populated them with the required members, but I when I start getting into the children I get the following error:

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

So I commented out the "children" objects, and was basically able to discern that it was blowing up when it tries to read the 'Location' part of the JSON object

in my code below, it concerns the part here:

               "__type":"Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",

for what its worth, the url is bad, but I don't care. I don't want to use that type (which apparently maps back to a schema at Microsoft's website). Is there a way to tell WCF to ignore that link? Its not like I can.

What Bing returns

{
   "authenticationResultCode":"ValidCredentials",
   "brandLogoUri":"http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png",
   "copyright":"Copyright © 2010 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.",
   "resourceSets":[
      {
         "estimatedTotal":1,
         "resources":[
            {
               "__type":"Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",
               "bbox":[
                  47.635884282429323,
                  -122.13737419709076,
                  47.643609717570676,
                  -122.12208780290925
               ],
               "name":"1 Microsoft Way, Redmond, WA 98052-8300",
               "point":{
                  "type":"Point",
                  "coordinates":[
                     47.639747,
                     -122.129731
                  ]
               },
               "address":{
                  "addressLine":"1 Microsoft Way",
                  "adminDistrict":"WA",
                  "adminDistrict2":"King County",
                  "countryRegion":"United States",
                  "formattedAddress":"1 Microsoft Way, Redmond, WA 98052-8300",
                  "locality":"Redmond",
                  "postalCode":"98052-8300"
               },
               "confidence":"High",
               "entityType":"Address"
            }
         ]
      }
   ],
   "statusCode":200,
   "statusDescription":"OK",
   "traceId":"43c6a4dc130749bbb14eb72bf12c4198 "
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蹲墙角沉默 2024-11-26 19:27:00

找到了。这是因为我必须适应数据契约中的 ref'd __type (它需要知道要使用什么类型。解决方案是这样的:

[DataContract(Namespace = "http://schemas.microsoft.com/search/local/ws/rest/v1", Name="Location")]

顺便说一句,我在这里找到了答案
在数据成员“__type”上反序列化 JSON 时出现问题

Found it. Its because I had to accomodate for the ref'd __type in my data contract (it needed to know what type to use. The solution was this:

[DataContract(Namespace = "http://schemas.microsoft.com/search/local/ws/rest/v1", Name="Location")]

btw, I found the answer here
: Problem with deserializing JSON on datamember "__type"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文