繁体   English   中英

从Google Places API反序列化JSON

[英]Deserialize json from google places api

我正在尝试反序列化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.predictions = null。

我在预测结构上犯了一个错误,但是我不确定是什么? 请帮忙。

我已经使用此站点从随机的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;

您的第一个问题是PlacesResult中的Predictions属性以大写字母开头(并且拼写为“ Preditions”),而json属性为小写字母。 如果您希望C#属性的名称与json属性的名称不同,则可以使用JsonProperty属性指定所需的确切名称。

例:

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

下一个问题是Prediction类中的types属性应该是字符串数组,而不是包含字符串数组的对象。

public string[] types { get; set; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM