.NET DataContractJsonSerializer,嵌套集合扔我一个循环 [英] .NET DataContractJsonSerializer, Nested collections throwing me for a loop
问题描述
我工作的一类由3个不同的供应商(只是为了进行比较)来获得一个地址的经度和纬度。
的注意,这可能看起来像很多来看待,但有些信息就在那里,以供参考,如果你想。下面的数据<小时方式>
是没有必要看的
我的谷歌地图DataContract就像一个冠军。但我的Bing和MapQuest的DataContracts打得并不那么好。我认为这个问题必须处理JSON用于Bing和MapQuest的的的嵌套集合的,我只是不知道如何对付他们。
下面是处理数据的功能。
''#因为从谷歌,MapQuest的,和Bing JSON响应有显着
''#不同,我们需要以不同的消费响应一点
''#每个API。
''###################################
''#BING被打破
''###################################
''#这里,我们处理兵提供商
如果供应商= apiProvider.Bing然后
昏暗的解析度= DirectCast(serializer.ReadObject(request.GetResponse()。GetResponseStream()),BingResponse)
昏暗的资源BingResponse.ResourceSet.Resource = res.resourceSets(0)的.resources(0)
朦胧点= resources.point
坐标
.latitude = point.coordinates(0)
.longitude = point.coordinates(1)
结束与 ''###################################
''#Google致力于如飞
''###################################
''#这里,我们处理谷歌提供
elseif的提供商= apiProvider.Google然后
昏暗的解析度= DirectCast(serializer.ReadObject(request.GetResponse()。GetResponseStream()),GoogleResponse)
昏暗的资源GoogleResponse.Result = res.results(0)
朦胧点= resources.geometry.location
坐标
.latitude = point.lat
.longitude = point.lng
结束与 ''###################################
''#Mapquest服务被打破
''###################################
''#这里,我们处理MapQuest的供应商
elseif的提供商= apiProvider.MapQuest然后
昏暗的解析度= DirectCast(serializer.ReadObject(request.GetResponse()。GetResponseStream()),MapQuestResponse)
昏暗的资源MapQuestResponse.Result = res.results(0)
朦胧点= resources.locations.latLng
坐标
.latitude = point.lat
.longitude = point.lng
结束与
万一
这是三个DataContracts
''#注释以SO修复code着色
&所述; DataContract()>
公共类BingResponse
<数据成员()>
公共财产resourceSets()作为的ResourceSet()
&所述; DataContract()>
公共类的ResourceSet
<数据成员()>
公共财产资源()作为资源()
< DataContract([命名空间]:=http://schemas.microsoft.com/search/local/ws/rest/v1,名称:=位置)>
公共类资源
<数据成员()>
公共属性点()作为m_Point
&所述; DataContract()>
公共类m_Point
<数据成员()>
公共财产坐标()作为字符串()
末级
末级
末级
末级&所述; DataContract()>
公共类GoogleResponse
<数据成员()>
公共财产的结果()作为结果()
&所述; DataContract()>
公共类结果
<数据成员()>
公共属性几何体m_Geometry
&所述; DataContract()>
公共类m_Geometry
<数据成员()>
公共属性Location作为m_location
&所述; DataContract()>
公共类m_location
<数据成员()>
公共属性纬度作为字符串
<数据成员()>
公共属性LNG作为字符串
末级
末级
末级
末级&所述; DataContract()>
公共类MapQuestResponse
<数据成员()>
公共财产的结果()作为结果()
&所述; DataContract()>
公共类结果
<数据成员()>
公共财产的地点作为m_Locations
&所述; DataContract()>
公共类m_Locations
<数据成员()>
公共财产的latLng作为m_latLng
&所述; DataContract()>
公共类m_latLng
<数据成员()>
公共属性纬度作为字符串
<数据成员()>
公共属性LNG作为字符串
末级
末级
末级
末级
在测试的 MapQuest的我得到这个错误
System.NullReferenceException:对象不设置到对象的实例。
在线
块引用>.latitude = point.lat
在测试的兵我得到这个错误
System.IndexOutOfRangeException:索引是该数组的范围之外。
在线
块引用>昏暗资源BingResponse.ResourceSet.Resource = res.resourceSets(0)的.resources(0)
仅供参考,我也将张贴的内容每个JSON响应的样子。
谷歌地图
{
状态:OK,
结果:[{
类型:[bus_station,transit_station],
的formatted_address:微软方式与放;微软ACS,华盛顿州雷德蒙市98052,USA,
address_components:[{
LONG_NAME:微软方式与放;微软ACS
SHORT_NAME:微软方式与放;微软ACS
类型:[bus_station,transit_station]
},{
LONG_NAME:雷德蒙,
SHORT_NAME:雷蒙德
类型:本地,政治]
},{
LONG_NAME:东方西雅图
SHORT_NAME:东方西雅图
类型:administrative_area_level_3,政治]
},{
LONG_NAME:王,
SHORT_NAME:王,
类型:administrative_area_level_2,政治]
},{
LONG_NAME:华盛顿号,
SHORT_NAME:WA
类型:administrative_area_level_1,政治]
},{
LONG_NAME:美的,
SHORT_NAME:美国,
类型:国家,政治]
},{
LONG_NAME:98052,
SHORT_NAME:98052,
类型:postal_ code]
}],
几何体:{
位置: {
纬度:47.6397018,
LNG:-122.1305080
},
LOCATION_TYPE:近似
视口:{
西南:{
纬度:47.6365542,
LNG:-122.1336556
},
东北:{
纬度:47.6428494,
LNG:-122.1273604
}
}
},
partial_match:真
},{
类型:[bus_station,transit_station],
的formatted_address:微软方式与放; NE第36街,华盛顿州雷德蒙市98052,USA,
address_components:[{
LONG_NAME:微软方式与放; NE第36街,
SHORT_NAME:微软方式与放; NE第36街,
类型:[bus_station,transit_station]
},{
LONG_NAME:雷德蒙,
SHORT_NAME:雷蒙德
类型:本地,政治]
},{
LONG_NAME:东方西雅图
SHORT_NAME:东方西雅图
类型:administrative_area_level_3,政治]
},{
LONG_NAME:王,
SHORT_NAME:王,
类型:administrative_area_level_2,政治]
},{
LONG_NAME:华盛顿号,
SHORT_NAME:WA
类型:administrative_area_level_1,政治]
},{
LONG_NAME:美的,
SHORT_NAME:美国,
类型:国家,政治]
},{
LONG_NAME:98052,
SHORT_NAME:98052,
类型:postal_ code]
}],
几何体:{
位置: {
纬度:47.6420593,
LNG:-122.1306990
},
LOCATION_TYPE:近似
视口:{
西南:{
纬度:47.6389117,
LNG:-122.1338466
},
东北:{
纬度:47.6452069,
LNG:-122.1275514
}
}
},
partial_match:真
},{
类型:[bus_station,transit_station],
的formatted_address:微软方式与放; NE 31ST街,华盛顿州雷德蒙市98052,USA,
address_components:[{
LONG_NAME:微软方式与放; NE 31ST ST,
SHORT_NAME:微软方式与放; NE 31ST ST,
类型:[bus_station,transit_station]
},{
LONG_NAME:雷德蒙,
SHORT_NAME:雷蒙德
类型:本地,政治]
},{
LONG_NAME:东方西雅图
SHORT_NAME:东方西雅图
类型:administrative_area_level_3,政治]
},{
LONG_NAME:王,
SHORT_NAME:王,
类型:administrative_area_level_2,政治]
},{
LONG_NAME:华盛顿号,
SHORT_NAME:WA
类型:administrative_area_level_1,政治]
},{
LONG_NAME:美的,
SHORT_NAME:美国,
类型:国家,政治]
},{
LONG_NAME:98052,
SHORT_NAME:98052,
类型:postal_ code]
}],
几何体:{
位置: {
纬度:47.6380959,
LNG:-122.1318050
},
LOCATION_TYPE:近似
视口:{
西南:{
纬度:47.6349483,
LNG:-122.1349526
},
东北:{
纬度:47.6412435,
LNG:-122.1286574
}
}
},
partial_match:真
},{
类型:路线]
的formatted_address:微软校园W¯¯ACRD,华盛顿州雷德蒙市,USA,
address_components:[{
LONG_NAME:微软校园W¯¯ACRD
SHORT_NAME:微软校园W¯¯ACRD
类型:路线]
},{
LONG_NAME:雷德蒙,
SHORT_NAME:雷蒙德
类型:本地,政治]
},{
LONG_NAME:东方西雅图
SHORT_NAME:东方西雅图
类型:administrative_area_level_3,政治]
},{
LONG_NAME:王,
SHORT_NAME:王,
类型:administrative_area_level_2,政治]
},{
LONG_NAME:华盛顿号,
SHORT_NAME:WA
类型:administrative_area_level_1,政治]
},{
LONG_NAME:美的,
SHORT_NAME:美国,
类型:国家,政治]
}],
几何体:{
位置: {
纬度:47.6582970,
LNG:-122.1414390
},
LOCATION_TYPE:GEOMETRIC_CENTER
视口:{
西南:{
纬度:47.6559394,
LNG:-122.1435435
},
东北:{
纬度:47.6622346,
LNG:-122.1372482
}
},
界限:{
西南:{
纬度:47.6577719,
LNG:-122.1433446
},
东北:{
纬度:47.6604021,
LNG:-122.1374471
}
}
},
partial_match:真
}]
}Bing地图
{authenticationResult code:ValidCredentials
brandLogoUri:http://dev.virtualearth.net/Branding/logo_powered_by.png,
版权:©2011 Microsoft及其供应商保留所有权利。该API无法访问,内容以及任何结果,不得使用,复制或传播以任何方式未经微软公司前preSS的书面许可。 。
resourceSets:[{estimatedTotal:1,
资源:[{__type:位置:http://schemas.microsoft.com/search/local/ws/rest/v1
地址:{addressLine:1 Microsoft方式
adminDistrict:WA,
COUNTRYREGION:美的,
formattedAddress:1 Microsoft方法,雷德蒙,WA 98052,
地方:雷蒙德
邮政code:98052
},
BBOX:[47.636682837606166,
-122.13698544771381,
47.644408272747519,
-122.12169881991093
]
自信:高,
的EntityType:地址,
名:1 Microsoft方法,雷德蒙,WA 98052,
点:{坐标:[47.640545555176843,
-122.12934213381237
]
类型:点
}
}]
}],
状态code:200,
状态说明:OK,
traceId:3d0dd6c4ea1f4c4ba6f9c7211a5abf75 | BAYM001222 | 02.00.82.2800 | BY2MSNVM001058
}MapQuest的
{信息:{版权:{imageAltText:©2011 MapQuest的公司,
图片网址:http://tile21.mqcdn.com/res/mqlogo.gif,
文:©2011 MapQuest的公司
},
消息:[],
状态code:0
},
选择:{ignoreLatLngInput:假的,
的maxResults:-1,
thumbMaps:真
},
结果:[{位置:[{adminArea1:美国,
adminArea1Type:国家,
adminArea3:WA,
adminArea3Type:国家,
adminArea4:金县
adminArea4Type:县,
adminArea5:雷德蒙,
adminArea5Type:城,
displayLatLng:{纬度:47.674197999999997,
LNG:-122.1203
},
dragPoint:假的,
地缘codeQuality:CITY,
地缘codeQuality code:A5XCX
的latLng:{纬度:47.674197999999997,
LNG:-122.1203
},
LINKID:0,
mapUrl: \"http://www.mapquestapi.com/staticmap/v3/getmap?type=map&size=225,160&pois=purple-1,47.674198,-122.1203,0,0|¢er=47.674198,-122.1203&zoom=9&key=Fmjtd|luu22q01ng,2n=o5-h6rnq&rand=-47567557\",
邮政code:,
sideOfStreet:N,
街头:,
类型:的
}],
providedLocation:{位置:1 Microsoft方法,WA雷德曼}
}]
}加成
解决方案有关地图的任务:
不应该
公共财产的地点作为m_Locations
是公共财产位置()作为m_Locations()
?我想这是在VB数组符号。这似乎你从数组对象采取的经纬度,而不是位置的实例阵列中的发现了一个<一个href=\"http://stackoverflow.com/questions/4115037/problem-with-deserializing-json-on-datamember-type\">similar问题与同样答复的数据合同(接受的答案)。寻找差异但不察觉他们尚未...
I'm working on a class to get the Latitude and Longitude of an address by 3 different providers (just for comparison sake).
Note, this might look like a lot to look at, but some info is just there for reference if you want. Data below the
<hr>
is not necessary to look at.My Google Maps DataContract works like a champ. But my Bing and MapQuest DataContracts aren't playing so nice. I think the issue has to deal with the Nested collections of the JSON for Bing and MapQuest, and I just don't know how to deal with them.
Here is the Function that processes the Data.
''# Because the JSON Response from Google, MapQuest, and Bing are drastically ''# different, we need to consume the response a little differently ''# for each API. ''# ################################## ''# BING IS BROKEN ''# ################################## ''# Here we're handling the Bing Provider If Provider = apiProvider.Bing Then Dim res = DirectCast(serializer.ReadObject(request.GetResponse().GetResponseStream()), BingResponse) Dim resources As BingResponse.ResourceSet.Resource = res.resourceSets(0).resources(0) Dim point = resources.point With coordinates .latitude = point.coordinates(0) .longitude = point.coordinates(1) End With ''# ################################## ''# GOOGLE WORKS LIKE A CHAMP ''# ################################## ''# Here we're handling the Google Provider ElseIf Provider = apiProvider.Google Then Dim res = DirectCast(serializer.ReadObject(request.GetResponse().GetResponseStream()), GoogleResponse) Dim resources As GoogleResponse.Result = res.results(0) Dim point = resources.geometry.location With coordinates .latitude = point.lat .longitude = point.lng End With ''# ################################## ''# MAPQUEST IS BROKEN ''# ################################## ''# Here we're handling the MapQuest Provider ElseIf Provider = apiProvider.MapQuest Then Dim res = DirectCast(serializer.ReadObject(request.GetResponse().GetResponseStream()), MapQuestResponse) Dim resources As MapQuestResponse.Result = res.results(0) Dim point = resources.locations.latLng With coordinates .latitude = point.lat .longitude = point.lng End With End If
And here are the three DataContracts
''# A comment to fix SO Code Coloring <DataContract()> Public Class BingResponse <DataMember()> Public Property resourceSets() As ResourceSet() <DataContract()> Public Class ResourceSet <DataMember()> Public Property resources() As Resource() <DataContract([Namespace]:="http://schemas.microsoft.com/search/local/ws/rest/v1", name:="Location")> Public Class Resource <DataMember()> Public Property point() As m_Point <DataContract()> Public Class m_Point <DataMember()> Public Property coordinates() As String() End Class End Class End Class End Class <DataContract()> Public Class GoogleResponse <DataMember()> Public Property results() As Result() <DataContract()> Public Class Result <DataMember()> Public Property geometry As m_Geometry <DataContract()> Public Class m_Geometry <DataMember()> Public Property location As m_location <DataContract()> Public Class m_location <DataMember()> Public Property lat As String <DataMember()> Public Property lng As String End Class End Class End Class End Class <DataContract()> Public Class MapQuestResponse <DataMember()> Public Property results() As Result() <DataContract()> Public Class Result <DataMember()> Public Property locations As m_Locations <DataContract()> Public Class m_Locations <DataMember()> Public Property latLng As m_latLng <DataContract()> Public Class m_latLng <DataMember()> Public Property lat As String <DataMember()> Public Property lng As String End Class End Class End Class End Class
When testing MapQuest I get this error
System.NullReferenceException: Object reference not set to an instance of an object. on line
.latitude = point.lat
When testing Bing I get this error
System.IndexOutOfRangeException: Index was outside the bounds of the array. on line
Dim resources As BingResponse.ResourceSet.Resource = res.resourceSets(0).resources(0)
FYI, I'll also post what the JSON Response for each looks like.
Google Maps
{ "status": "OK", "results": [ { "types": [ "bus_station", "transit_station" ], "formatted_address": "Microsoft Way & Microsoft ACS, Redmond, WA 98052, USA", "address_components": [ { "long_name": "Microsoft Way & Microsoft ACS", "short_name": "Microsoft Way & Microsoft ACS", "types": [ "bus_station", "transit_station" ] }, { "long_name": "Redmond", "short_name": "Redmond", "types": [ "locality", "political" ] }, { "long_name": "East Seattle", "short_name": "East Seattle", "types": [ "administrative_area_level_3", "political" ] }, { "long_name": "King", "short_name": "King", "types": [ "administrative_area_level_2", "political" ] }, { "long_name": "Washington", "short_name": "WA", "types": [ "administrative_area_level_1", "political" ] }, { "long_name": "United States", "short_name": "US", "types": [ "country", "political" ] }, { "long_name": "98052", "short_name": "98052", "types": [ "postal_code" ] } ], "geometry": { "location": { "lat": 47.6397018, "lng": -122.1305080 }, "location_type": "APPROXIMATE", "viewport": { "southwest": { "lat": 47.6365542, "lng": -122.1336556 }, "northeast": { "lat": 47.6428494, "lng": -122.1273604 } } }, "partial_match": true }, { "types": [ "bus_station", "transit_station" ], "formatted_address": "Microsoft Way & NE 36th St, Redmond, WA 98052, USA", "address_components": [ { "long_name": "Microsoft Way & NE 36th St", "short_name": "Microsoft Way & NE 36th St", "types": [ "bus_station", "transit_station" ] }, { "long_name": "Redmond", "short_name": "Redmond", "types": [ "locality", "political" ] }, { "long_name": "East Seattle", "short_name": "East Seattle", "types": [ "administrative_area_level_3", "political" ] }, { "long_name": "King", "short_name": "King", "types": [ "administrative_area_level_2", "political" ] }, { "long_name": "Washington", "short_name": "WA", "types": [ "administrative_area_level_1", "political" ] }, { "long_name": "United States", "short_name": "US", "types": [ "country", "political" ] }, { "long_name": "98052", "short_name": "98052", "types": [ "postal_code" ] } ], "geometry": { "location": { "lat": 47.6420593, "lng": -122.1306990 }, "location_type": "APPROXIMATE", "viewport": { "southwest": { "lat": 47.6389117, "lng": -122.1338466 }, "northeast": { "lat": 47.6452069, "lng": -122.1275514 } } }, "partial_match": true }, { "types": [ "bus_station", "transit_station" ], "formatted_address": "Microsoft Way & NE 31ST St, Redmond, WA 98052, USA", "address_components": [ { "long_name": "Microsoft Way & NE 31ST St", "short_name": "Microsoft Way & NE 31ST St", "types": [ "bus_station", "transit_station" ] }, { "long_name": "Redmond", "short_name": "Redmond", "types": [ "locality", "political" ] }, { "long_name": "East Seattle", "short_name": "East Seattle", "types": [ "administrative_area_level_3", "political" ] }, { "long_name": "King", "short_name": "King", "types": [ "administrative_area_level_2", "political" ] }, { "long_name": "Washington", "short_name": "WA", "types": [ "administrative_area_level_1", "political" ] }, { "long_name": "United States", "short_name": "US", "types": [ "country", "political" ] }, { "long_name": "98052", "short_name": "98052", "types": [ "postal_code" ] } ], "geometry": { "location": { "lat": 47.6380959, "lng": -122.1318050 }, "location_type": "APPROXIMATE", "viewport": { "southwest": { "lat": 47.6349483, "lng": -122.1349526 }, "northeast": { "lat": 47.6412435, "lng": -122.1286574 } } }, "partial_match": true }, { "types": [ "route" ], "formatted_address": "Microsoft w Campus Acrd, Redmond, WA, USA", "address_components": [ { "long_name": "Microsoft w Campus Acrd", "short_name": "Microsoft w Campus Acrd", "types": [ "route" ] }, { "long_name": "Redmond", "short_name": "Redmond", "types": [ "locality", "political" ] }, { "long_name": "East Seattle", "short_name": "East Seattle", "types": [ "administrative_area_level_3", "political" ] }, { "long_name": "King", "short_name": "King", "types": [ "administrative_area_level_2", "political" ] }, { "long_name": "Washington", "short_name": "WA", "types": [ "administrative_area_level_1", "political" ] }, { "long_name": "United States", "short_name": "US", "types": [ "country", "political" ] } ], "geometry": { "location": { "lat": 47.6582970, "lng": -122.1414390 }, "location_type": "GEOMETRIC_CENTER", "viewport": { "southwest": { "lat": 47.6559394, "lng": -122.1435435 }, "northeast": { "lat": 47.6622346, "lng": -122.1372482 } }, "bounds": { "southwest": { "lat": 47.6577719, "lng": -122.1433446 }, "northeast": { "lat": 47.6604021, "lng": -122.1374471 } } }, "partial_match": true } ] }
Bing Maps
{ "authenticationResultCode" : "ValidCredentials", "brandLogoUri" : "http://dev.virtualearth.net/Branding/logo_powered_by.png", "copyright" : "Copyright © 2011 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", "address" : { "addressLine" : "1 Microsoft Way", "adminDistrict" : "WA", "countryRegion" : "United States", "formattedAddress" : "1 Microsoft Way, Redmond, WA 98052", "locality" : "Redmond", "postalCode" : "98052" }, "bbox" : [ 47.636682837606166, -122.13698544771381, 47.644408272747519, -122.12169881991093 ], "confidence" : "High", "entityType" : "Address", "name" : "1 Microsoft Way, Redmond, WA 98052", "point" : { "coordinates" : [ 47.640545555176843, -122.12934213381237 ], "type" : "Point" } } ] } ], "statusCode" : 200, "statusDescription" : "OK", "traceId" : "3d0dd6c4ea1f4c4ba6f9c7211a5abf75|BAYM001222|02.00.82.2800|BY2MSNVM001058" }
MapQuest
{ "info" : { "copyright" : { "imageAltText" : "© 2011 MapQuest, Inc.", "imageUrl" : "http://tile21.mqcdn.com/res/mqlogo.gif", "text" : "© 2011 MapQuest, Inc." }, "messages" : [ ], "statuscode" : 0 }, "options" : { "ignoreLatLngInput" : false, "maxResults" : -1, "thumbMaps" : true }, "results" : [ { "locations" : [ { "adminArea1" : "US", "adminArea1Type" : "Country", "adminArea3" : "WA", "adminArea3Type" : "State", "adminArea4" : "King County", "adminArea4Type" : "County", "adminArea5" : "Redmond", "adminArea5Type" : "City", "displayLatLng" : { "lat" : 47.674197999999997, "lng" : -122.1203 }, "dragPoint" : false, "geocodeQuality" : "CITY", "geocodeQualityCode" : "A5XCX", "latLng" : { "lat" : 47.674197999999997, "lng" : -122.1203 }, "linkId" : 0, "mapUrl" : "http://www.mapquestapi.com/staticmap/v3/getmap?type=map&size=225,160&pois=purple-1,47.674198,-122.1203,0,0|¢er=47.674198,-122.1203&zoom=9&key=Fmjtd|luu22q01ng,2n=o5-h6rnq&rand=-47567557", "postalCode" : "", "sideOfStreet" : "N", "street" : "", "type" : "s" } ], "providedLocation" : { "location" : "1 Microsoft Way, Redmon WA" } } ] }
Addition
解决方案For map quest:
Shouldn't
Public Property locations As m_Locations
bePublic Property locations() As m_Locations()
? I assume this is array notation in VB. It would seem you're taking the latLng from the Array object and not the instance of the location in the array.Found a similar question with a data contract (accepted answer) for the same response. Looking for differences but not spotting them yet...
这篇关于.NET DataContractJsonSerializer,嵌套集合扔我一个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!