asp.net - .NET DataContractJsonSerializer, Nested collections throwing me for a loop -


i'm working on class latitude , longitude of address 3 different providers (just comparison sake).

note, might lot at, info there reference if want. data below <hr> not necessary at.

my google maps datacontract works champ. bing , mapquest datacontracts aren't playing nice. think issue has deal nested collections of json bing , mapquest, , don't know how deal them.

here function processes data.

        ''# because json response google, mapquest, , bing drastically         ''# different, need consume response little differently         ''# each api.            ''# ##################################         ''# bing broken         ''# ##################################         ''# here we're handling bing provider         if provider = apiprovider.bing             dim res = directcast(serializer.readobject(request.getresponse().getresponsestream()), bingresponse)             dim resources bingresponse.resourceset.resource = res.resourcesets(0).resources(0)             dim point = resources.point             coordinates                 .latitude = point.coordinates(0)                 .longitude = point.coordinates(1)             end            ''# ##################################         ''# google works champ         ''# ##################################             ''# here we're handling google provider         elseif provider = apiprovider.google             dim res = directcast(serializer.readobject(request.getresponse().getresponsestream()), googleresponse)             dim resources googleresponse.result = res.results(0)             dim point = resources.geometry.location             coordinates                 .latitude = point.lat                 .longitude = point.lng             end            ''# ##################################         ''# mapquest broken         ''# ##################################             ''# here we're handling mapquest provider         elseif provider = apiprovider.mapquest             dim res = directcast(serializer.readobject(request.getresponse().getresponsestream()), mapquestresponse)             dim resources mapquestresponse.result = res.results(0)             dim point = resources.locations.latlng             coordinates                 .latitude = point.lat                 .longitude = point.lng             end         end if 

and here 3 datacontracts

''# comment fix code coloring <datacontract()> public class bingresponse     <datamember()>     public property resourcesets() resourceset()     <datacontract()>     public class resourceset         <datamember()>         public property resources() resource()         <datacontract([namespace]:="http://schemas.microsoft.com/search/local/ws/rest/v1", name:="location")>         public class resource             <datamember()>             public property point() m_point             <datacontract()>             public class m_point                 <datamember()>                 public property coordinates() string()             end class         end class     end class end class  <datacontract()> public class googleresponse     <datamember()>     public property results() result()     <datacontract()>     public class result         <datamember()>         public property geometry m_geometry         <datacontract()>         public class m_geometry             <datamember()>             public property location m_location             <datacontract()>             public class m_location                 <datamember()>                 public property lat string                 <datamember()>                 public property lng string             end class         end class     end class end class  <datacontract()> public class mapquestresponse     <datamember()>     public property results() result()     <datacontract()>     public class result         <datamember()>         public property locations m_locations         <datacontract()>         public class m_locations             <datamember()>             public property latlng m_latlng             <datacontract()>             public class m_latlng                 <datamember()>                 public property lat string                 <datamember()>                 public property lng string             end class         end class     end class end class 

when testing mapquest error

system.nullreferenceexception: object reference not set instance of object. on line

.latitude = point.lat 

when testing bing error

system.indexoutofrangeexception: index outside bounds of array. on line

dim resources bingresponse.resourceset.resource = res.resourcesets(0).resources(0) 

fyi, i'll post json response 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 , suppliers. rights reserved. api cannot accessed , content , results may not used, reproduced or transmitted in manner without express written permission 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|&center=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

screenshot

for map quest:

shouldn't public property locations m_locations public property locations() m_locations()? assume array notation in vb. seem you're taking latlng array object , not instance of location in array.

found similar question data contract (accepted answer) same response. looking differences not spotting them yet...


Comments