【问题标题】:I need list of US states and counties of the state [closed]我需要美国州和州县的名单[关闭]
【发布时间】:2018-05-08 13:57:46
【问题描述】:

我需要在正在处理的应用程序中包含美国当地的州和州县的列表。那么有人可以告诉我在哪里可以找到该列表以 Json 或 .plist 格式下载。

谢谢

【问题讨论】:

    标签: android ios json plist


    【解决方案1】:

    只要你下定决心,你就可以为所欲为。

    我已经为您编写了一个工具,可以从已知的更新来源(维基百科)中提取这些数据。

    代码可以从Github下载。

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Net.Http;
    using HtmlAgilityPack;
    using Newtonsoft.Json;
    
    namespace GetCitiesCounties
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(@"Hitting Wikipedia");
    
                var uri = new Uri("https://en.wikipedia.org/wiki/List_of_United_States_counties_and_county_equivalents");
                var client = new HttpClient();
                var rs = client.GetAsync(uri).Result;
                if (rs.IsSuccessStatusCode)
                {
                    var htmlContent = rs.Content.ReadAsStringAsync().Result;
                    var htmlDoc = new HtmlDocument();
                    htmlDoc.LoadHtml(htmlContent);
                    var list = new List<dynamic>();
                    var nodes = htmlDoc.DocumentNode.SelectNodes("//table[@class='wikitable sortable']//tr");
    
                    Console.WriteLine(@"Processing Rows");
    
                    int rowIndex = 0;
                    foreach (var row in nodes)
                    {
                        if (rowIndex++ > 0)
                        {
                            var county = row.SelectNodes("td")[1].InnerText;
                            var state = row.SelectNodes("td")[2].InnerText;
    
                            list.Add(new
                            {
                                County = county,
                                State = state
                            });
                        }
                    }
    
                    var json = JsonConvert.SerializeObject(list);
                    File.WriteAllText(@"C:\test.json", json);
    
                    Console.WriteLine(@"Done, extracted cities and states to json file C:\test.json");
                    Console.ReadLine();
                }
            }
        }
    }
    

    这是结果的 sn-p。它显示了美国的每个县及其所属的州。

    [
      {
        "County": "Autauga County",
        "State": "Alabama"
      },
      {
        "County": "Baldwin County",
        "State": "Alabama"
      },
      {
        "County": "Barbour County",
        "State": "Alabama"
      }
    ]
    

    【讨论】:

      【解决方案2】:

      你可以在这里找到列表:https://www.webtoolz.online/resources/usa-list-of-states 此列表包含 JSON 格式的州名和缩写。

      【讨论】:

        猜你喜欢
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-13
        • 2014-10-07
        • 1970-01-01
        • 1970-01-01
        • 2020-08-20
        相关资源
        最近更新 更多