元素':item'包含映射到名称'http:// ...:Location'的类型的数据。反序列化器不知道任何映射到thi的类型

时间:2011-07-13 20:31:45

标签: wcf json bing

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

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

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

所以我注释掉了“children”对象,并且当它试图读取JSON对象的“Location”部分时基本上能够辨别出它正在爆炸

在下面的代码中,它涉及到这里的部分:

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

它的价值,网址是坏的,但我不在乎。我不想使用那种类型(显然可以映射回微软网站的架构)。有没有办法告诉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 "
}

1 个答案:

答案 0 :(得分:7)

找到它。因为我必须在我的数据合同中容纳ref_ __type(它需要知道使用什么类型。解决方案是:

[DataContract(Namespace = "http://schemas.microsoft.com/search/local/ws/rest/v1", Name="Location")]
顺便说一句,我在这里找到了答案 :Problem with deserializing JSON on datamember "__type"