我有这个国家/地区的 json 文件,我想使用关键字国家/地区进行解析。请帮忙,因为我是 swift 的新手,我真的很想了解更多。谢谢。
[
{"country":"Afghanistan","code":"AF"},{"country":"\u00c5land Islands","code":"AX"},
{"country":"Albania","code":"AL"},
{"country":"Algeria","code":"DZ"},
{"country":"American Samoa","code":"AS"},
{"country":"AndorrA","code":"AD"},
{"country":"Angola","code":"AO"},
{"country":"Anguilla","code":"AI"},
{"country":"Antarctica","code":"AQ"},
{"country":"Antigua and Barbuda","code":"AG"},
{"country":"Argentina","code":"AR"},
{"country":"Armenia","code":"AM"},
{"country":"Aruba","code":"AW"},
{"country":"Australia","code":"AU"},
{"country":"Austria","code":"AT"},
{"country":"Azerbaijan","code":"AZ"},
{"country":"Bahamas","code":"BS"},
{"country":"Bahrain","code":"BH"},
{"country":"Bangladesh","code":"BD"},
{"country":"Barbados","code":"BB"},
{"country":"Belarus","code":"BY"},
{"country":"Belgium","code":"BE"},
{"country":"Belize","code":"BZ"},
{"country":"Benin","code":"BJ"},
{"country":"Bermuda","code":"BM"},
{"country":"Bhutan","code":"BT"},
{"country":"Bolivia","code":"BO"},
{"country":"Bosnia and Herzegovina","code":"BA"},
{"country":"Botswana","code":"BW"},
{"country":"Bouvet Island","code":"BV"},
{"country":"Brazil","code":"BR"},
{"country":"British Indian Ocean Territory","code":"IO"},
{"country":"Brunei Darussalam","code":"BN"},
{"country":"Bulgaria","code":"BG"},
{"country":"Burkina Faso","code":"BF"},
{"country":"Burundi","code":"BI"},
{"country":"Cambodia","code":"KH"},
{"country":"Cameroon","code":"CM"},
{"country":"Canada","code":"CA"},
{"country":"Cape Verde","code":"CV"},
{"country":"Cayman Islands","code":"KY"},
{"country":"Central African Republic","code":"CF"},{"country":"Chad","code":"TD"},
{"country":"Chile","code":"CL"},
{"country":"China","code":"CN"},
{"country":"Christmas Island","code":"CX"},
{"country":"Cocos (Keeling) Islands","code":"CC"},{"country":"Colombia","code":"CO"},
{"country":"Comoros","code":"KM"},
{"country":"Congo","code":"CG"},
{"country":"Congo, The Democratic Republic of the","code":"CD"},
{"country":"Cook Islands","code":"CK"},
{"country":"Costa Rica","code":"CR"},
{"country":"Cote D\"Ivoire","code":"CI"},
{"country":"Croatia","code":"HR"},
{"country":"Cuba","code":"CU"},
{"country":"Cyprus","code":"CY"},
{"country":"Czech Republic","code":"CZ"},
{"country":"Denmark","code":"DK"},
{"country":"Djibouti","code":"DJ"},
{"country":"Dominica","code":"DM"},
{"country":"Dominican Republic","code":"DO"},
{"country":"Ecuador","code":"EC"},
{"country":"Egypt","code":"EG"},
{"country":"El Salvador","code":"SV"},
{"country":"Equatorial Guinea","code":"GQ"},{"country":"Eritrea","code":"ER"},{"country":"Estonia","code":"EE"},{"country":"Ethiopia","code":"ET"},{"country":"Falkland Islands (Malvinas)","code":"FK"},{"country":"Faroe Islands","code":"FO"},{"country":"Fiji","code":"FJ"},{"country":"Finland","code":"FI"},{"country":"France","code":"FR"},{"country":"French Guiana","code":"GF"},{"country":"French Polynesia","code":"PF"},{"country":"French Southern Territories","code":"TF"},{"country":"Gabon","code":"GA"},{"country":"Gambia","code":"GM"},{"country":"Georgia","code":"GE"},{"country":"Germany","code":"DE"},{"country":"Ghana","code":"GH"},{"country":"Gibraltar","code":"GI"},{"country":"Greece","code":"GR"},{"country":"Greenland","code":"GL"},{"country":"Grenada","code":"GD"},{"country":"Guadeloupe","code":"GP"},{"country":"Guam","code":"GU"},{"country":"Guatemala","code":"GT"},{"country":"Guernsey","code":"GG"},{"country":"Guinea","code":"GN"},{"country":"Guinea-Bissau","code":"GW"},{"country":"Guyana","code":"GY"},{"country":"Haiti","code":"HT"},{"country":"Heard Island and Mcdonald Islands","code":"HM"},{"country":"Holy See (Vatican City State)","code":"VA"},{"country":"Honduras","code":"HN"},{"country":"Hong Kong","code":"HK"},{"country":"Hungary","code":"HU"},{"country":"Iceland","code":"IS"},{"country":"India","code":"IN"},{"country":"Indonesia","code":"ID"},{"country":"Iran, Islamic Republic Of","code":"IR"},{"country":"Iraq","code":"IQ"},{"country":"Ireland","code":"IE"},{"country":"Isle of Man","code":"IM"},{"country":"Israel","code":"IL"},{"country":"Italy","code":"IT"},{"country":"Jamaica","code":"JM"},{"country":"Japan","code":"JP"},{"country":"Jersey","code":"JE"},{"country":"Jordan","code":"JO"},{"country":"Kazakhstan","code":"KZ"},{"country":"Kenya","code":"KE"},{"country":"Kiribati","code":"KI"},{"country":"Korea, Democratic People\"S Republic of","code":"KP"},{"country":"Korea, Republic of","code":"KR"},{"country":"Kuwait","code":"KW"},{"country":"Kyrgyzstan","code":"KG"},{"country":"Lao People\"S Democratic Republic","code":"LA"},{"country":"Latvia","code":"LV"},{"country":"Lebanon","code":"LB"},{"country":"Lesotho","code":"LS"},{"country":"Liberia","code":"LR"},{"country":"Libyan Arab Jamahiriya","code":"LY"},{"country":"Liechtenstein","code":"LI"},{"country":"Lithuania","code":"LT"},{"country":"Luxembourg","code":"LU"},{"country":"Macao","code":"MO"},{"country":"Macedonia, The Former Yugoslav Republic of","code":"MK"},{"country":"Madagascar","code":"MG"},{"country":"Malawi","code":"MW"},{"country":"Malaysia","code":"MY"},{"country":"Maldives","code":"MV"},{"country":"Mali","code":"ML"},{"country":"Malta","code":"MT"},{"country":"Marshall Islands","code":"MH"},{"country":"Martinique","code":"MQ"},{"country":"Mauritania","code":"MR"},{"country":"Mauritius","code":"MU"},{"country":"Mayotte","code":"YT"},{"country":"Mexico","code":"MX"},{"country":"Micronesia, Federated States of","code":"FM"},{"country":"Moldova, Republic of","code":"MD"},{"country":"Monaco","code":"MC"},{"country":"Mongolia","code":"MN"},{"country":"Montserrat","code":"MS"},{"country":"Morocco","code":"MA"},{"country":"Mozambique","code":"MZ"},{"country":"Myanmar","code":"MM"},{"country":"Namibia","code":"NA"},{"country":"Nauru","code":"NR"},{"country":"Nepal","code":"NP"},{"country":"Netherlands","code":"NL"},{"country":"Netherlands Antilles","code":"AN"},{"country":"New Caledonia","code":"NC"},{"country":"New Zealand","code":"NZ"},{"country":"Nicaragua","code":"NI"},{"country":"Niger","code":"NE"},{"country":"Nigeria","code":"NG"},{"country":"Niue","code":"NU"},{"country":"Norfolk Island","code":"NF"},{"country":"Northern Mariana Islands","code":"MP"},{"country":"Norway","code":"NO"},{"country":"Oman","code":"OM"},{"country":"Pakistan","code":"PK"},{"country":"Palau","code":"PW"},{"country":"Palestinian Territory, Occupied","code":"PS"},{"country":"Panama","code":"PA"},{"country":"Papua New Guinea","code":"PG"},{"country":"Paraguay","code":"PY"},{"country":"Peru","code":"PE"},{"country":"Philippines","code":"PH"},{"country":"Pitcairn","code":"PN"},{"country":"Poland","code":"PL"},{"country":"Portugal","code":"PT"},{"country":"Puerto Rico","code":"PR"},{"country":"Qatar","code":"QA"},{"country":"Reunion","code":"RE"},{"country":"Romania","code":"RO"},{"country":"Russian Federation","code":"RU"},{"country":"RWANDA","code":"RW"},{"country":"Saint Helena","code":"SH"},{"country":"Saint Kitts and Nevis","code":"KN"},{"country":"Saint Lucia","code":"LC"},{"country":"Saint Pierre and Miquelon","code":"PM"},{"country":"Saint Vincent and the Grenadines","code":"VC"},{"country":"Samoa","code":"WS"},{"country":"San Marino","code":"SM"},{"country":"Sao Tome and Principe","code":"ST"},{"country":"Saudi Arabia","code":"SA"},{"country":"Senegal","code":"SN"},{"country":"Serbia and Montenegro","code":"CS"},{"country":"Seychelles","code":"SC"},{"country":"Sierra Leone","code":"SL"},{"country":"Singapore","code":"SG"},{"country":"Slovakia","code":"SK"},{"country":"Slovenia","code":"SI"},{"country":"Solomon Islands","code":"SB"},{"country":"Somalia","code":"SO"},{"country":"South Africa","code":"ZA"},{"country":"South Georgia and the South Sandwich Islands","code":"GS"},{"country":"Spain","code":"ES"},{"country":"Sri Lanka","code":"LK"},{"country":"Sudan","code":"SD"},{"country":"Suriname","code":"SR"},{"country":"Svalbard and Jan Mayen","code":"SJ"},{"country":"Swaziland","code":"SZ"},{"country":"Sweden","code":"SE"},{"country":"Switzerland","code":"CH"},{"country":"Syrian Arab Republic","code":"SY"},{"country":"Taiwan","code":"TW"},{"country":"Tajikistan","code":"TJ"},{"country":"Tanzania, United Republic of","code":"TZ"},{"country":"Thailand","code":"TH"},{"country":"Timor-Leste","code":"TL"},{"country":"Togo","code":"TG"},{"country":"Tokelau","code":"TK"},{"country":"Tonga","code":"TO"},{"country":"Trinidad and Tobago","code":"TT"},{"country":"Tunisia","code":"TN"},{"country":"Turkey","code":"TR"},{"country":"Turkmenistan","code":"TM"},{"country":"Turks and Caicos Islands","code":"TC"},{"country":"Tuvalu","code":"TV"},{"country":"Uganda","code":"UG"},{"country":"Ukraine","code":"UA"},{"country":"United Arab Emirates","code":"AE"},{"country":"United Kingdom","code":"GB"},{"country":"United States","code":"US"},{"country":"United States Minor Outlying Islands","code":"UM"},{"country":"Uruguay","code":"UY"},{"country":"Uzbekistan","code":"UZ"},{"country":"Vanuatu","code":"VU"},{"country":"Venezuela","code":"VE"},{"country":"Viet Nam","code":"VN"},{"country":"Virgin Islands, British","code":"VG"},{"country":"Virgin Islands, U.S.","code":"VI"},
{"country":"Wallis and Futuna","code":"WF"},
{"country":"Western Sahara","code":"EH"},
{"country":"Yemen","code":"YE"},{"country":"Zambia","code":"ZM"},
{"country":"Zimbabwe","code":"ZW"}
]
最佳答案
JSON 字符串是一个包含[String:String]类型字典的数组
获取字符串的NSData表示
let data = jsonString.dataUsingEncoding(NSUTF8StringEncoding)!
将数据反序列化为 Swift 数组
let json = try! NSJSONSerialization.JSONObjectWithData(data, options: []) as! [[String:String]]
使用map 函数获取所有国家/地区值
let countries = json.map{ $0["country"]! }
如果字符串是从网络接收到的,你需要添加适当的错误处理
if let data = jsonString.dataUsingEncoding(NSUTF8StringEncoding) {
do {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as! [[String:String]]
let countries = json.map{ $0["country"]! }
} catch let error as NSError {
print(error)
}
}
关于ios - 你如何在swift中解析json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35811481/
我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我正在使用ruby1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"