元素 ':item' 包含映射到名称"http://...:Location"的类型中的数据。反序列化程序没有映射到



我正在编写代码来使用Bing Geocode服务(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",

不管它的价值,url是坏的,但我不在乎。我不想使用这种类型(它显然映射回微软网站上的模式)。是否有办法告诉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 "
}

找到了。这是因为我必须在我的数据契约中适应被引用的__type(它需要知道使用什么类型)。解决方案如下:

[DataContract(Namespace = "http://schemas.microsoft.com/search/local/ws/rest/v1", Name="Location")]
顺便说一句,我在这里找到了答案:在数据成员"__type"上反序列化JSON的问题

最新更新