Paul Stanley
Paul Stanley

Reputation: 2161

Deserialize json from google places api

I am trying to deserialize this Json response from the Google Places Api. Here is the json:

{
   "predictions" : [
      {
         "description" : "Gosport, United Kingdom",
         "id" : "424e88b1dd50db4669e490c8a0ef9c411bea30bf",
         "matched_substrings" : [
            {
               "length" : 7,
               "offset" : 0
            }
         ],
         "reference" : "CjQvAAAAIlHmEkN9OGEsbUvly-cKNUup9OT_1lR1R4LZ51yNjgKxBMIQisFVOTTWl-LiiTqxEhC_p2OfnrlyacwvXk-jZj4VGhTFx0rd3p7Tk0bgCcYT7DdZlFeshQ",
         "terms" : [
            {
               "offset" : 0,
               "value" : "Gosport"
            },
            {
               "offset" : 9,
               "value" : "United Kingdom"
            }
         ],
         "types" : [ "locality", "political", "geocode" ]
      },
      {
         "description" : "Gosport Ferry Terminal, Gosport, United Kingdom",
         "id" : "298f74f3ba700467bd9a47c1212e10c8134ff503",
         "matched_substrings" : [
            {
               "length" : 7,
               "offset" : 0
            }
         ],
         "reference" : "CkQ9AAAAY7mGBQK1zUixFtlRIk7nv_s2BVh134OVmog9zIA5eEQuR8w1E3bpLxRb5cUClI4n-rhQMi7TOgXarOmNt3RjWBIQQLb678JEvbQ6d5lOQMvuUhoUaPRDwagXm7gLzXV9Y8U_Lm01qqg",
         "terms" : [
            {
               "offset" : 0,
               "value" : "Gosport Ferry Terminal"
            },
            {
               "offset" : 24,
               "value" : "Gosport"
            },
            {
               "offset" : 33,
               "value" : "United Kingdom"
            }
         ],
         "types" : [ "transit_station", "establishment", "geocode" ]
      },
      {
         "description" : "Gosport War Memorial Hospital, Bury Road, Gosport, United Kingdom",
         "id" : "199ed00865e8afdc1f9aa0be4db14a638bfc9399",
         "matched_substrings" : [
            {
               "length" : 7,
               "offset" : 0
            }
         ],
         "reference" : "ClRPAAAAH6s84cNExgG1z6H_AaWrQylKxKR8jMrxcx4unHzcgEwYXLFtKevIhpSfVIAU7HimPUquG2ghCsV4zkLDammJOE-CZk1A1bW0_QDIo2I4YhgSELM8YL9zhZXYtWFTg-YBIf0aFGQjXBzJN_imeJEpImBoJV5n7hJW",
         "terms" : [
            {
               "offset" : 0,
               "value" : "Gosport War Memorial Hospital"
            },
            {
               "offset" : 31,
               "value" : "Bury Road"
            },
            {
               "offset" : 42,
               "value" : "Gosport"
            },
            {
               "offset" : 51,
               "value" : "United Kingdom"
            }
         ],
         "types" : [ "establishment", "geocode" ]
      },
      {
         "description" : "Gosport Ice Rink, Forest Way, Gosport, United Kingdom",
         "id" : "f59973493605c03b55513bdf09f9c1c12edb8575",
         "matched_substrings" : [
            {
               "length" : 7,
               "offset" : 0
            }
         ],
         "reference" : "ClREAAAACjJkMsZfWVW7uf5hWxPL3kSnsKkEUbEfi03Cj6Sv48WO54lMc9JbR9zk6c21OWVBKiTRz4w4IpfcKjedsXLLxMBfk_zozk_luezbSa7VtbQSEH38WCT9rLtU9lBdBzNNBGYaFO7yjHUiZzCBChF7uCeu1ClOfVmm",
         "terms" : [
            {
               "offset" : 0,
               "value" : "Gosport Ice Rink"
            },
            {
               "offset" : 18,
               "value" : "Forest Way"
            },
            {
               "offset" : 30,
               "value" : "Gosport"
            },
            {
               "offset" : 39,
               "value" : "United Kingdom"
            }
         ],
         "types" : [ "establishment" ]
      },
      {
         "description" : "Gosport Ferry Ltd, South Street, Gosport, United Kingdom",
         "id" : "362ac1a81cabc5f32299bd79e21f8cace49533da",
         "matched_substrings" : [
            {
               "length" : 7,
               "offset" : 0
            }
         ],
         "reference" : "ClRHAAAAWsRXg2IEaNw0EUdPwnlMaSEjJXP1W8AYGyuMzn_ghQmRoW-OyXdUDREZ5Bk_adxd3itCxnNtmWgXMJiPJ_n_-bfErAhCSgj04hoT0jNl8j0SELC1t32oWdabliIQZGk8ktkaFOpGIsE94D-GjXSQx0IpibA76ame",
         "terms" : [
            {
               "offset" : 0,
               "value" : "Gosport Ferry Ltd"
            },
            {
               "offset" : 19,
               "value" : "South Street"
            },
            {
               "offset" : 33,
               "value" : "Gosport"
            },
            {
               "offset" : 42,
               "value" : "United Kingdom"
            }
         ],
         "types" : [ "establishment" ]
      }
   ],
   "status" : "OK"
}

Here are my objects that I will try to deserialize into:

        public class PlacesResult
        {
            public string Status { get; set; }
            public Prediction[] Preditions { get; set; }
        }
        public class Prediction
        {
            public string description { get; set; }
            public string id { get; set; }
            public matched_substring[] matched_substrings { get; set; }
            public string reference { get; set; }
            public Terms[] terms { get; set; }
            public Types types { get; set; }
        }
        public class Terms
        {
            public string offset { get; set; }
            public string value { get; set; }
        }
        public class Types
        {
            public string[] types { get; set; }                    
        }
        public class matched_substring
        {
            public int length { get; set; }
            public int offset { get; set; }
        }

Here is my code (using NewtonSoft json.NET)

json = new System.Net.WebClient().DownloadString(url);

PlacesResult resultz = JsonConvert.DeserializeObject<PlacesResult>(json);

I am getting resultz.status = "OK" resultz.predictions = null.

I am making a mistake with predictions structure but I am not sure what? Please help.

Upvotes: 1

Views: 1985

Answers (3)

Yishai Galatzer
Yishai Galatzer

Reputation: 8862

I've used this site to come up with my class structure from a random Json file. http://json2csharp.com/

It's a starting point, but it should be more useful to other viewers of this post, than just plain fixing the classes for you :)

Edit (2/2018) - Apparently there is also a vs extension

Upvotes: 7

parachutingturtle
parachutingturtle

Reputation: 146

Your first problem was that your Predictions property in PlacesResult starts with an uppercase letter (and it's misspelled as "Preditions"), while the json property is lowercase. If you prefer to have different names for your C# properties than the json properties, you can use JsonProperty attributes to specify the exact name expected.

Example:

[JsonProperty(PropertyName="predictions")]
public Prediction[] Preditions { get; set; }

The next problem was that the types property in the Prediction class is supposed to be an array of strings, not an object that contains a string array.

public string[] types { get; set; }

Upvotes: 2

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

Your classes for json generated using http://json2csharp.com:

public class MatchedSubstring
{
    public int length { get; set; }
    public int offset { get; set; }
}

public class Term
{
    public int offset { get; set; }
    public string value { get; set; }
}

public class Prediction
{
    public string description { get; set; }
    public string id { get; set; }
    public List<MatchedSubstring> matched_substrings { get; set; }
    public string reference { get; set; }
    public List<Term> terms { get; set; }
    public List<string> types { get; set; }
}

public class RootObject
{
    public List<Prediction> predictions { get; set; }
    public string status { get; set; }
}

and access like this:

 var Jsonobject = JsonConvert.DeserializeObject<RootObject>(json);


 List<Prediction> list = Jsonobject .predictions;

Upvotes: 5

Related Questions