c# - 从 google places api 反序列化 json

标签 c# json

我正在尝试反序列化来自 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 = "确定" resultz.predictions = null.

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

最佳答案

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

这是一个起点,但它应该对本文的其他浏览者更有用,而不仅仅是简单地为您修复类:)

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

关于c# - 从 google places api 反序列化 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23583693/

相关文章:

c# - WPF 图像作为工具提示 - DPI 问题

c# - XML - 如何在单个节点而不是整个文档中获取子节点?

json - grails使用json构建器构建数组

java - 无法解析 JSON

ios - 在不使用第三方库的情况下将 NSData JSON 对象发送到 Web 服务

c# - SQLite。修复 sqlite-net-wp8 项目依赖项

c# - 如何解决 Xamarin.Android Java 互操作的缓慢问题

c# - 使用 List.Sort(Comparison<T> comparison) 在 C# 中对列表进行排序

json - 正确访问json文件

ios - 我们可以在 Swift 上重用结构吗?或者还有其他办法吗?