从google places api反序列化json

本文关键字:反序列化 json api places google | 更新日期: 2023-09-27 18:02:45

我正试图从Google Places Api反序列化此Json响应。下面是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"
}

下面是我要反序列化成的对象:

        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; }
        }

这是我的代码(使用NewtonSoft json.NET)

json = new System.Net.WebClient().DownloadString(url);
PlacesResult resultz = JsonConvert.DeserializeObject<PlacesResult>(json);

我得到了resultz。status = "OK"resultz。预测= null.

我正在做一个错误的预测结构,但我不确定什么?请帮助。

从google places api反序列化json

我使用这个网站从一个随机的Json文件中得出我的类结构。http://json2csharp.com/

这是一个起点,但它应该对这篇文章的其他观众更有用,而不仅仅是简单地为你修复类:)

编辑(2/2018)-显然还有一个vs扩展

使用http://json2csharp.com:生成的json类

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; }
}

和这样的访问:

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

 List<Prediction> list = Jsonobject .predictions;

您的第一个问题是您的Predictions属性在PlacesResult中以大写字母开头(并且它被拼错为" Predictions "),而json属性是小写的。如果你希望c#属性的名称与json属性的名称不同,你可以使用JsonProperty属性来指定期望的确切名称。

的例子:

[JsonProperty(PropertyName="predictions")]
public Prediction[] Preditions { get; set; }
下一个问题是,Prediction类中的types属性应该是一个字符串数组,而不是一个包含字符串数组的对象。

public string[] types { get; set; }