Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do DataContracts work? - Deserialize Json
    primarykey
    data
    text
    <p>I grabbed an example off of <a href="https://stackoverflow.com/questions/4115037/problem-with-deserializing-json-on-datamember-type/4119681#4119681">this SO question</a>, and built my own custom Google Maps object used for deserializing the json object.</p> <p>Now the code works like a champ, but I just need an explanation on why/how it works. Does the serializer "try" to match up names, or is something else going on.</p> <p><strong>What exactly is this doing?</strong></p> <p>Here's the <strong>working</strong> code.</p> <pre><code>Imports System.Net Imports System.Web Imports System.Runtime.Serialization Imports System.Runtime.Serialization.Json Imports System.Web.Script.Serialization Namespace Utilities.Apis Public NotInheritable Class GoogleGeolocate Private Const googleUrl As String = "http://maps.googleapis.com/maps/api/geocode/json?address={0}&amp;sensor=false" Private Sub New() End Sub Public Shared Function GetLatLon(ByVal address As String) As String ''# This is just here to prevent "placeholder" data from being submitted. If address = "6789 university drive" Then Return Nothing End If address = HttpUtility.UrlEncode(address) Dim url = String.Format(googleUrl, address) Dim request = DirectCast(HttpWebRequest.Create(url), HttpWebRequest) request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate") request.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate Dim serializer As New DataContractJsonSerializer(GetType(GoogleResponse)) Dim res = DirectCast(serializer.ReadObject(request.GetResponse().GetResponseStream()), GoogleResponse) Dim resources As GoogleResponse.Result = res.results(0) Dim point = resources.geometry.location.lat Dim latlon As New GeolocationLatLon With latlon .latitude = resources.geometry.location.lat .longitude = resources.geometry.location.lng End With Dim jsonSerializer = New JavaScriptSerializer Return jsonSerializer.Serialize(latlon) End Function End Class &lt;DataContract()&gt; Public Class GoogleResponse &lt;DataMember()&gt; Public Property results() As Result() &lt;DataContract()&gt; Public Class Result &lt;DataMember()&gt; Public Property geometry As m_Geometry &lt;DataContract()&gt; Public Class m_Geometry &lt;DataMember()&gt; Public Property location As m_location &lt;DataContract()&gt; Public Class m_location &lt;DataMember()&gt; Public Property lat As String &lt;DataMember()&gt; Public Property lng As String End Class End Class End Class End Class End Namespace </code></pre> <p>And here's the GeolocationLatLon Poco</p> <pre><code>Public Class GeolocationLatLon Public latitude As String Public longitude As String End Class </code></pre> <p>When I call the code, it's really quite simple.<br> <em>note, this is an MVC controller, that has nothing to do "really" with the question other than to show what I'm doing</em></p> <pre><code> Function GeoLocation(ByVal address As String) As ContentResult Return New ContentResult With {.Content = GoogleGeolocate.GetLatLon(address), .ContentType = "application/json"} End Function </code></pre> <p>And the final result is</p> <blockquote> <p>{"latitude":"50.124300","longitude":"-114.4979990"}</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload