carled
asked on
Cannot get json from google geocoder to deserialize to class in asp.net, it's close but not quite there!
Hi there. I am successfully reading in the return from the google reverse geocoding service and this code:
Dim url As String = String.Format(GeoCodeUrlFo rmat, HttpUtility.UrlEncode(latl ng))
Dim request As Net.HttpWebRequest = CType(HttpWebRequest.Creat e(url), HttpWebRequest)
Dim response As HttpWebResponse = CType(request.GetResponse, HttpWebResponse)
Dim responseStream As Stream = response.GetResponseStream
Dim jsonResponse As String = String.Empty
Dim sr As StreamReader = New StreamReader(response.GetR esponseStr eam)
jsonResponse = sr.ReadToEnd
returns this string in jsonResponse:
{
"status": "OK",
"results": [ {
"types": [ "street_address" ],
"formatted_address": "53-127 Swithland Rd, Rothley, Leicestershire LE7 7, UK",
"address_components": [ {
"long_name": "53-127",
"short_name": "53-127",
"types": [ "street_number" ]
}, {
"long_name": "Swithland Rd",
"short_name": "Swithland Rd",
"types": [ "route" ]
}, {
"long_name": "Rothley",
"short_name": "Rothley",
"types": [ "locality", "political" ]
}, {
"long_name": "Charnwood",
"short_name": "Charnwood",
"types": [ "administrative_area_level _3", "political" ]
}, {
"long_name": "Leicestershire",
"short_name": "Leics",
"types": [ "administrative_area_level _2", "political" ]
}, {
"long_name": "England",
"short_name": "England",
"types": [ "administrative_area_level _1", "political" ]
}, {
"long_name": "United Kingdom",
"short_name": "GB",
"types": [ "country", "political" ]
}, {
"long_name": "LE7 7",
"short_name": "LE7 7",
"types": [ "postal_code" ]
} ],
"geometry": {
"location": {
"lat": 52.7138499,
"lng": -1.1597912
},
"location_type": "RANGE_INTERPOLATED",
"viewport": {
"southwest": {
"lat": 52.7098329,
"lng": -1.1633441
},
"northeast": {
"lat": 52.7161281,
"lng": -1.1570489
}
},
"bounds": {
"southwest": {
"lat": 52.7099090,
"lng": -1.1619330
},
"northeast": {
"lat": 52.7160520,
"lng": -1.1584600
}
}
}
}, {
"types": [ "locality", "political" ],
"formatted_address": "Rothley, Leicestershire, UK",
"address_components": [ {
"long_name": "Rothley",
"short_name": "Rothley",
"types": [ "locality", "political" ]
}, {
"long_name": "Charnwood",
"short_name": "Charnwood",
"types": [ "administrative_area_level _3", "political" ]
}, {
"long_name": "Leicestershire",
"short_name": "Leics",
"types": [ "administrative_area_level _2", "political" ]
}, {
"long_name": "England",
"short_name": "England",
"types": [ "administrative_area_level _1", "political" ]
}, {
"long_name": "United Kingdom",
"short_name": "GB",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": 52.7173201,
"lng": -1.1294207
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": 52.6927790,
"lng": -1.1743950
},
"northeast": {
"lat": 52.7293340,
"lng": -1.1183210
}
},
"bounds": {
"southwest": {
"lat": 52.6927790,
"lng": -1.1743950
},
"northeast": {
"lat": 52.7293340,
"lng": -1.1183210
}
}
}
}, {
"types": [ "postal_code" ],
"formatted_address": "Mountsorrel, Leicestershire LE7 7, UK",
"address_components": [ {
"long_name": "LE7 7",
"short_name": "LE7 7",
"types": [ "postal_code" ]
}, {
"long_name": "Mountsorrel",
"short_name": "Mountsorrel",
"types": [ "locality", "political" ]
}, {
"long_name": "Charnwood",
"short_name": "Charnwood",
"types": [ "administrative_area_level _3", "political" ]
}, {
"long_name": "Leicestershire",
"short_name": "Leics",
"types": [ "administrative_area_level _2", "political" ]
}, {
"long_name": "England",
"short_name": "England",
"types": [ "administrative_area_level _1", "political" ]
}, {
"long_name": "United Kingdom",
"short_name": "GB",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": 52.6916895,
"lng": -1.1698908
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": 52.6559029,
"lng": -1.2129934
},
"northeast": {
"lat": 52.7304609,
"lng": -1.1122492
}
},
"bounds": {
"southwest": {
"lat": 52.6559029,
"lng": -1.2129934
},
"northeast": {
"lat": 52.7304609,
"lng": -1.1122492
}
}
}
}, {
"types": [ "administrative_area_level _3", "political" ],
"formatted_address": "Charnwood, Leicestershire, UK",
"address_components": [ {
"long_name": "Charnwood",
"short_name": "Charnwood",
"types": [ "administrative_area_level _3", "political" ]
}, {
"long_name": "Leicestershire",
"short_name": "Leics",
"types": [ "administrative_area_level _2", "political" ]
}, {
"long_name": "England",
"short_name": "England",
"types": [ "administrative_area_level _1", "political" ]
}, {
"long_name": "United Kingdom",
"short_name": "GB",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": 52.7401228,
"lng": -1.1405925
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": 52.6538650,
"lng": -1.3350460
},
"northeast": {
"lat": 52.8247250,
"lng": -0.9472430
}
},
"bounds": {
"southwest": {
"lat": 52.6538650,
"lng": -1.3350460
},
"northeast": {
"lat": 52.8247250,
"lng": -0.9472430
}
}
}
}, {
"types": [ "administrative_area_level _2", "political" ],
"formatted_address": "Leicestershire, UK",
"address_components": [ {
"long_name": "Leicestershire",
"short_name": "Leics",
"types": [ "administrative_area_level _2", "political" ]
}, {
"long_name": "England",
"short_name": "England",
"types": [ "administrative_area_level _1", "political" ]
}, {
"long_name": "United Kingdom",
"short_name": "GB",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": 52.7401228,
"lng": -1.1405925
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": 52.3921670,
"lng": -1.5975470
},
"northeast": {
"lat": 52.9776180,
"lng": -0.6641060
}
},
"bounds": {
"southwest": {
"lat": 52.3921670,
"lng": -1.5975470
},
"northeast": {
"lat": 52.9776180,
"lng": -0.6641060
}
}
}
}, {
"types": [ "administrative_area_level _1", "political" ],
"formatted_address": "England, United Kingdom",
"address_components": [ {
"long_name": "England",
"short_name": "England",
"types": [ "administrative_area_level _1", "political" ]
}, {
"long_name": "United Kingdom",
"short_name": "GB",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": 52.0190289,
"lng": -0.7704274
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": 49.8825312,
"lng": -6.3651943
},
"northeast": {
"lat": 55.8116485,
"lng": 1.7689260
}
},
"bounds": {
"southwest": {
"lat": 49.8825312,
"lng": -6.3651943
},
"northeast": {
"lat": 55.8116485,
"lng": 1.7689260
}
}
}
}, {
"types": [ "country", "political" ],
"formatted_address": "United Kingdom",
"address_components": [ {
"long_name": "United Kingdom",
"short_name": "GB",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": 55.3780510,
"lng": -3.4359730
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": 49.1626564,
"lng": -8.6626630
},
"northeast": {
"lat": 60.8616500,
"lng": 1.7689260
}
},
"bounds": {
"southwest": {
"lat": 49.1626564,
"lng": -8.6626630
},
"northeast": {
"lat": 60.8616500,
"lng": 1.7689260
}
}
}
} ]
}
I have this structure defined:
<Serializable()> _
Public Class rGeoCodeCls
Public status As String
Public results() As List(Of rGeoCodeResult)
End Class
<Serializable()> _
Public Class rGeoCodeResult
Public types() As List(Of rGeoCodeResultType)
Public formatted_address As String
Public address_components() As List(Of rGeoCodeResultAddressCompo nent)
Public geometry As rGeoCodeResultGeometry
End Class
<Serializable()> _
Public Class rGeoCodeResultType
Public resType As String
End Class
<Serializable()> _
Public Class rGeoCodeResultAddressCompo nent
Public long_name As String
Public short_name As String
Public types() As List(Of rGeoCodeResultType)
End Class
<Serializable()> _
Public Class rGeoCodeResultGeometry
Public location As rGeoCodeResultLocation
Public location_type As String
Public viewport As rGeoCodeResultBounds
Public bounds As rGeoCodeResultBounds
End Class
<Serializable()> _
Public Class rGeoCodeResultLocation
Public latitude As Double
Public longitude As Double
End Class
<Serializable()> _
Public Class rGeoCodeResultBounds
Public southwest As rGeoCodeResultLocation
Public northeast As rGeoCodeResultLocation
End Class
And when I run this code:
Dim ser As System.Web.Script.Serializ ation.Java ScriptSeri alizer = New System.Web.Script.Serializ ation.Java ScriptSeri alizer
Dim gRes As rGeoCodeCls = ser.Deserialize(Of rGeoCodeCls)(jsonResponse)
If gRes.status = "OK" Then
'do something
Else
'do something else
End If
I sort of get the start of the right result but there are data missing... :-(
I get:
gRes:
results {length=7}
(0) - Count=0
(1) - Count=0
(2) - Count=0
(3) - Count=0
(4) - Count=0
(5) - Count=0
(6) - Count=0
status - "OK"
So the "status" bit comes through alright, but none of the other stuff... please can you shove me in the right direction on this? Thanks.
Dim url As String = String.Format(GeoCodeUrlFo
Dim request As Net.HttpWebRequest = CType(HttpWebRequest.Creat
Dim response As HttpWebResponse = CType(request.GetResponse,
Dim responseStream As Stream = response.GetResponseStream
Dim jsonResponse As String = String.Empty
Dim sr As StreamReader = New StreamReader(response.GetR
jsonResponse = sr.ReadToEnd
returns this string in jsonResponse:
{
"status": "OK",
"results": [ {
"types": [ "street_address" ],
"formatted_address": "53-127 Swithland Rd, Rothley, Leicestershire LE7 7, UK",
"address_components": [ {
"long_name": "53-127",
"short_name": "53-127",
"types": [ "street_number" ]
}, {
"long_name": "Swithland Rd",
"short_name": "Swithland Rd",
"types": [ "route" ]
}, {
"long_name": "Rothley",
"short_name": "Rothley",
"types": [ "locality", "political" ]
}, {
"long_name": "Charnwood",
"short_name": "Charnwood",
"types": [ "administrative_area_level
}, {
"long_name": "Leicestershire",
"short_name": "Leics",
"types": [ "administrative_area_level
}, {
"long_name": "England",
"short_name": "England",
"types": [ "administrative_area_level
}, {
"long_name": "United Kingdom",
"short_name": "GB",
"types": [ "country", "political" ]
}, {
"long_name": "LE7 7",
"short_name": "LE7 7",
"types": [ "postal_code" ]
} ],
"geometry": {
"location": {
"lat": 52.7138499,
"lng": -1.1597912
},
"location_type": "RANGE_INTERPOLATED",
"viewport": {
"southwest": {
"lat": 52.7098329,
"lng": -1.1633441
},
"northeast": {
"lat": 52.7161281,
"lng": -1.1570489
}
},
"bounds": {
"southwest": {
"lat": 52.7099090,
"lng": -1.1619330
},
"northeast": {
"lat": 52.7160520,
"lng": -1.1584600
}
}
}
}, {
"types": [ "locality", "political" ],
"formatted_address": "Rothley, Leicestershire, UK",
"address_components": [ {
"long_name": "Rothley",
"short_name": "Rothley",
"types": [ "locality", "political" ]
}, {
"long_name": "Charnwood",
"short_name": "Charnwood",
"types": [ "administrative_area_level
}, {
"long_name": "Leicestershire",
"short_name": "Leics",
"types": [ "administrative_area_level
}, {
"long_name": "England",
"short_name": "England",
"types": [ "administrative_area_level
}, {
"long_name": "United Kingdom",
"short_name": "GB",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": 52.7173201,
"lng": -1.1294207
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": 52.6927790,
"lng": -1.1743950
},
"northeast": {
"lat": 52.7293340,
"lng": -1.1183210
}
},
"bounds": {
"southwest": {
"lat": 52.6927790,
"lng": -1.1743950
},
"northeast": {
"lat": 52.7293340,
"lng": -1.1183210
}
}
}
}, {
"types": [ "postal_code" ],
"formatted_address": "Mountsorrel, Leicestershire LE7 7, UK",
"address_components": [ {
"long_name": "LE7 7",
"short_name": "LE7 7",
"types": [ "postal_code" ]
}, {
"long_name": "Mountsorrel",
"short_name": "Mountsorrel",
"types": [ "locality", "political" ]
}, {
"long_name": "Charnwood",
"short_name": "Charnwood",
"types": [ "administrative_area_level
}, {
"long_name": "Leicestershire",
"short_name": "Leics",
"types": [ "administrative_area_level
}, {
"long_name": "England",
"short_name": "England",
"types": [ "administrative_area_level
}, {
"long_name": "United Kingdom",
"short_name": "GB",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": 52.6916895,
"lng": -1.1698908
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": 52.6559029,
"lng": -1.2129934
},
"northeast": {
"lat": 52.7304609,
"lng": -1.1122492
}
},
"bounds": {
"southwest": {
"lat": 52.6559029,
"lng": -1.2129934
},
"northeast": {
"lat": 52.7304609,
"lng": -1.1122492
}
}
}
}, {
"types": [ "administrative_area_level
"formatted_address": "Charnwood, Leicestershire, UK",
"address_components": [ {
"long_name": "Charnwood",
"short_name": "Charnwood",
"types": [ "administrative_area_level
}, {
"long_name": "Leicestershire",
"short_name": "Leics",
"types": [ "administrative_area_level
}, {
"long_name": "England",
"short_name": "England",
"types": [ "administrative_area_level
}, {
"long_name": "United Kingdom",
"short_name": "GB",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": 52.7401228,
"lng": -1.1405925
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": 52.6538650,
"lng": -1.3350460
},
"northeast": {
"lat": 52.8247250,
"lng": -0.9472430
}
},
"bounds": {
"southwest": {
"lat": 52.6538650,
"lng": -1.3350460
},
"northeast": {
"lat": 52.8247250,
"lng": -0.9472430
}
}
}
}, {
"types": [ "administrative_area_level
"formatted_address": "Leicestershire, UK",
"address_components": [ {
"long_name": "Leicestershire",
"short_name": "Leics",
"types": [ "administrative_area_level
}, {
"long_name": "England",
"short_name": "England",
"types": [ "administrative_area_level
}, {
"long_name": "United Kingdom",
"short_name": "GB",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": 52.7401228,
"lng": -1.1405925
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": 52.3921670,
"lng": -1.5975470
},
"northeast": {
"lat": 52.9776180,
"lng": -0.6641060
}
},
"bounds": {
"southwest": {
"lat": 52.3921670,
"lng": -1.5975470
},
"northeast": {
"lat": 52.9776180,
"lng": -0.6641060
}
}
}
}, {
"types": [ "administrative_area_level
"formatted_address": "England, United Kingdom",
"address_components": [ {
"long_name": "England",
"short_name": "England",
"types": [ "administrative_area_level
}, {
"long_name": "United Kingdom",
"short_name": "GB",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": 52.0190289,
"lng": -0.7704274
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": 49.8825312,
"lng": -6.3651943
},
"northeast": {
"lat": 55.8116485,
"lng": 1.7689260
}
},
"bounds": {
"southwest": {
"lat": 49.8825312,
"lng": -6.3651943
},
"northeast": {
"lat": 55.8116485,
"lng": 1.7689260
}
}
}
}, {
"types": [ "country", "political" ],
"formatted_address": "United Kingdom",
"address_components": [ {
"long_name": "United Kingdom",
"short_name": "GB",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": 55.3780510,
"lng": -3.4359730
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": 49.1626564,
"lng": -8.6626630
},
"northeast": {
"lat": 60.8616500,
"lng": 1.7689260
}
},
"bounds": {
"southwest": {
"lat": 49.1626564,
"lng": -8.6626630
},
"northeast": {
"lat": 60.8616500,
"lng": 1.7689260
}
}
}
} ]
}
I have this structure defined:
<Serializable()> _
Public Class rGeoCodeCls
Public status As String
Public results() As List(Of rGeoCodeResult)
End Class
<Serializable()> _
Public Class rGeoCodeResult
Public types() As List(Of rGeoCodeResultType)
Public formatted_address As String
Public address_components() As List(Of rGeoCodeResultAddressCompo
Public geometry As rGeoCodeResultGeometry
End Class
<Serializable()> _
Public Class rGeoCodeResultType
Public resType As String
End Class
<Serializable()> _
Public Class rGeoCodeResultAddressCompo
Public long_name As String
Public short_name As String
Public types() As List(Of rGeoCodeResultType)
End Class
<Serializable()> _
Public Class rGeoCodeResultGeometry
Public location As rGeoCodeResultLocation
Public location_type As String
Public viewport As rGeoCodeResultBounds
Public bounds As rGeoCodeResultBounds
End Class
<Serializable()> _
Public Class rGeoCodeResultLocation
Public latitude As Double
Public longitude As Double
End Class
<Serializable()> _
Public Class rGeoCodeResultBounds
Public southwest As rGeoCodeResultLocation
Public northeast As rGeoCodeResultLocation
End Class
And when I run this code:
Dim ser As System.Web.Script.Serializ
Dim gRes As rGeoCodeCls = ser.Deserialize(Of rGeoCodeCls)(jsonResponse)
If gRes.status = "OK" Then
'do something
Else
'do something else
End If
I sort of get the start of the right result but there are data missing... :-(
I get:
gRes:
results {length=7}
(0) - Count=0
(1) - Count=0
(2) - Count=0
(3) - Count=0
(4) - Count=0
(5) - Count=0
(6) - Count=0
status - "OK"
So the "status" bit comes through alright, but none of the other stuff... please can you shove me in the right direction on this? Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.