Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting google places details JSON into a C# Object with JavaScriptDeserializer

I'm currently trying to convert JSON from a google places details api call into a c# object through deserialization. I have successfully done this using the same process for both a google geocode call, and a google places call.

These are my google places, and places details calls:

           string uri = "https://maps.googleapis.com/maps/api/place/radarsearch/json?";

           uri += "key=" + mapkey + "&";
           uri += "location=" + lat.ToString() + "," + lon.ToString() + "&";
           uri += "radius=" + radius.ToString() + "&";
           uri += "types=restaurant";

           string detailUri = "https://maps.googleapis.com/maps/api/place/details/json?";

           string results = client.DownloadString(uri);

           JavaScriptSerializer js = new JavaScriptSerializer();

           PlacesResponse placesresults = js.Deserialize<PlacesResponse>(results);

           for(int i = 0; i < placesresults.Results.Length; i++ )
           {

               detailUri += "placeid=" + placesresults.Results[i].Place_Id + "&key=" + mapkey;

               string details = client.DownloadString(detailUri);                  

               DetailResponse detailresults = js.Deserialize<DetailResponse>(details);                  

                   restaurants.Add(new Restaurant()
                       {
                           Name = detailresults.Results.Name,
                           PlaceID = detailresults.Results.Name,
                           AddressNumber = detailresults.Results.Name,
                           PhoneNumber = detailresults.Results.Name,
                           Rating = detailresults.Results.Name,
                           WebSite = detailresults.Results.Name
                       });

           }

The models I use for google places (working for place ID) are:

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using System.Threading.Tasks;

   namespace DetroitEatz.Models
   {
       public class PlacesResponse
       {
    public string Status {get;set;}
    public PlacesResults[] Results { get; set; }
   }
   public class PlacesResults
   {
    public PlacesGeometry Geometry { get; set; }
    public string Place_Id { get; set; }

   }
    public class PlacesGeometry
   {
    PlacesLocation Location {get; set;}
   }
    public class PlacesLocation
   {
    public double Lat {get;set;}
    public double Lon {get;set;}
  }

  }

The models I use for the details (results null) call are:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;

 namespace DetroitEatz.Models
 {
    public class DetailResponse
    {
    public string Status {get;set;}
    public DetailResult Results { get; set; }
    }
    public class DetailResult
    {
    public string Name { get; set; }
    public string Website { get; set; }
    public double Rating { get; set; }
    public string Formatted_Address { get; set; }
    public string Formatted_Phone_Number { get; set; }
    public DetailAddress_Components[] Adress_Components { get; set; }
    }
    public class DetailAddress_Components
    {
    public string[] Types { get; set; }
    public string Long_Name { get; set; }
    public string Short_Name { get; set; }

    }
    public class DetailGeometry
    {
    public DetailLocation Location { get; set; }
 }
public class DetailLocation
{
    public string Lat {get;set;}
    public string Lon {get;set;}
}
}

The problem I am having is that when I deserialize the json string from the details call, the "Results" property is showing up null.

I would really appreciate any help or suggestions to fixing this problem.

like image 304
Travis Kean Avatar asked Nov 28 '25 11:11

Travis Kean


1 Answers

Actually, I fixed the issue using http://json2csharp.com/, in case anyone else has this type of problem. Not positive on the "why", but I suspect it had to either do with the naming convention I had, or me not including every aspect of the results.

like image 117
Travis Kean Avatar answered Nov 29 '25 23:11

Travis Kean