2

I thought I could simply build a quick class and use JSON.Net to deserialize the result, and it is kind of working but I think I am blowing it on my class structure:

public class Location
    {
        public string lat { get; set; }
        public string lng { get; set; }
    }

    public class Geometry
    {
        public Location location { get; set; }
    }

    public class Json
    {
        public Results results { get; set; }
    }

    public class Results
    {
        public Json json { get; set; }
        public Geometry geometry { get; set; }
    }

    public class Query
    {
        public Results results { get; set; }
    }

    public class RootObject
    {
        public Query query { get; set; }
    }

Here is a sample of what get's returned from Google:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Parkway",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara",
               "short_name" : "Santa Clara",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.42244920,
               "lng" : -122.08506440
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.42379818029150,
                  "lng" : -122.0837154197085
               },
               "southwest" : {
                  "lat" : 37.42110021970850,
                  "lng" : -122.0864133802915
               }
            }
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

I am missing simple here I know it, anyone?

3
  • 1
    it is kind of working but I think I am blowing it on my class structure. What does this mean? what is not working? What error do you get? Commented Aug 2, 2013 at 15:51
  • @I4V I think he means that he's only getting some of the data when he deserializes, and other fields are probably null. This will happen if the class structure doesn't match the JSON, as is clearly the case here. Commented Aug 2, 2013 at 15:56
  • 1
    @BrianRogers I can see this from your answer. But wouldn't it be better, if we wouldn't have to guess what the problem is? Commented Aug 2, 2013 at 16:02

2 Answers 2

5

Try using json2csharp.com to generate your classes. Using this tool, the class structure comes out like this:

public class AddressComponent
{
    public string long_name { get; set; }
    public string short_name { get; set; }
    public List<string> types { get; set; }
}

public class Location
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Northeast
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Southwest
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Viewport
{
    public Northeast northeast { get; set; }
    public Southwest southwest { get; set; }
}

public class Geometry
{
    public Location location { get; set; }
    public string location_type { get; set; }
    public Viewport viewport { get; set; }
}

public class Result
{
    public List<AddressComponent> address_components { get; set; }
    public string formatted_address { get; set; }
    public Geometry geometry { get; set; }
    public List<string> types { get; set; }
}

public class RootObject
{
    public List<Result> results { get; set; }
    public string status { get; set; }
}

Note that you can simiplify this a little: the generated Northeast and Southwest classes are identical to Location, so you can replace them with Location where they are used inside the Viewport class.

Sign up to request clarification or add additional context in comments.

Comments

1

In newer versions of Visual Studio, you can select Edit > Paste Special > Paste JSON as Classes to create your classes.

Here's the result created with the latest version of Google Maps API's returned JSON example:

public class Rootobject
{
    public Result[] results { get; set; }
    public string status { get; set; }
}

public class Result
{
    public Address_Components[] address_components { get; set; }
    public string formatted_address { get; set; }
    public Geometry geometry { get; set; }
    public string place_id { get; set; }
    public string[] types { get; set; }
}

public class Geometry
{
    public Location location { get; set; }
    public string location_type { get; set; }
    public Viewport viewport { get; set; }
}

public class Location
{
    public float lat { get; set; }
    public float lng { get; set; }
}

public class Viewport
{
    public Northeast northeast { get; set; }
    public Southwest southwest { get; set; }
}

public class Northeast
{
    public float lat { get; set; }
    public float lng { get; set; }
}

public class Southwest
{
    public float lat { get; set; }
    public float lng { get; set; }
}

public class Address_Components
{
    public string long_name { get; set; }
    public string short_name { get; set; }
    public string[] types { get; set; }
}

You can rename Rootobject to whatever you need it to be.

There's also an XML option in the same menu to perform the same action with XML.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.