你如何在swift中解析json?

时间:2016-03-05 07:41:14

标签: ios arrays json swift parsing

我有这个json国家/地区的文件,我想使用关键字country解析。请帮忙,因为我是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"}
]

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

使用 NSJSONSerialization

if let data = yourJSONString.dataUsingEncoding(NSUTF8StringEncoding) {
    let json = try? NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
    print(json)
}