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

Kev*_*eus 2 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",
Run Code Online (Sandbox Code Playgroud)

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

Kev*_*eus 7

找到了.因为我必须在我的数据合同中容纳ref'd __type(它需要知道要使用的类型.解决方案是:

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

顺便说一下,我在这里找到了答案:在datamember"__type"上反序列化JSON的问题