如何从swift4中的字符串中获取特定键值

时间:2017-11-29 09:43:20

标签: ios iphone swift sorting

我有来自xml的字符串响应,我得到所有值,但我需要一个名为" District_NameEng"的特定键值。我是怎么做到的。

func parser(_ parser:XMLParser,foundCharacters string:String){

from" string" =我得到了这个回复

我的回答是:

[{"Loc_District":8119,"districtname":"अजमेर ","District_NameEng":"AJMER"},{"Loc_District":8104,"districtname":"अलवर ","District_NameEng":"ALWAR"},{"Loc_District":8125,"districtname":"बांसवाड़ा","District_NameEng":"BANSWARA"},{"Loc_District":8128,"districtname":"बारां ","District_NameEng":"BARAN"},{"Loc_District":8115,"districtname":"बाड़मेर ","District_NameEng":"BARMER"},{"Loc_District":8105,"districtname":"भरतपुर ","District_NameEng":"BHARATPUR"},{"Loc_District":8122,"districtname":"भीलवाडा ","District_NameEng":"BHILWARA"},{"Loc_District":8101,"districtname":"बीकानेर ","District_NameEng":"BIKANER"},{"Loc_District":8121,"districtname":"बून्दी ","District_NameEng":"BUNDI"},{"Loc_District":8126,"districtname":"चित्तौड़गढ़ ","District_NameEng":"CHITTORGARH"},{"Loc_District":8102,"districtname":"चूरू ","District_NameEng":"CHURU"},{"Loc_District":8109,"districtname":"दौसा ","District_NameEng":"DAUSA"},{"Loc_District":8106,"districtname":"धौलपुर ","District_NameEng":"DHOLPUR"},{"Loc_District":8124,"districtname":"डूंगरपुर ","District_NameEng":"DUNGARPUR"},{"Loc_District":8099,"districtname":"गंगानगर ","District_NameEng":"GANGANAGAR"},{"Loc_District":8100,"districtname":"हनुमानगढ ","District_NameEng":"HANUMANGARH"},{"Loc_District":8110,"districtname":"जयपुर ","District_NameEng":"JAIPUR"},{"Loc_District":8114,"districtname":"जैसलमेर ","District_NameEng":"JAISALMER"},{"Loc_District":8116,"districtname":"जालोर ","District_NameEng":"JALORE"},{"Loc_District":8129,"districtname":"झालावाड ","District_NameEng":"JHALAWAR"},{"Loc_District":8103,"districtname":"झुंझुंनु ","District_NameEng":"JHUNJHUNU"},{"Loc_District":8113,"districtname":"जोधपुर ","District_NameEng":"JODHPUR"},{"Loc_District":8107,"districtname":"करौली ","District_NameEng":"KAROULI"},{"Loc_District":8127,"districtname":"कोटा ","District_NameEng":"KOTA"},{"Loc_District":8112,"districtname":"नागौर ","District_NameEng":"NAGAUR"},{"Loc_District":8118,"districtname":"पाली ","District_NameEng":"PALI"},{"Loc_District":8131,"districtname":"प्रतापगढ ","District_NameEng":"PRATAPGARH"},{"Loc_District":8123,"districtname":"राजसमन्द ","District_NameEng":"RAJSAMAND"},{"Loc_District":8108,"districtname":"सवाई माधोपुर ","District_NameEng":"SAWAI MADHOPUR"},{"Loc_District":8111,"districtname":"सीकर ","District_NameEng":"SIKAR"},{"Loc_District":8117,"districtname":"सिरोही ","District_NameEng":"SIROHI"},{"Loc_District":8120,"districtname":"टोंक ","District_NameEng":"TONK"},{"Loc_District":8130,"districtname":"उदयपुर ","District_NameEng":"UDAIPUR"}]

3 个答案:

答案 0 :(得分:0)

这是字典的JSON数组,您可以使用map获取特定键的所有值,如下所示。

let arrayValues = mainArray.map{$0["District_NameEng"] as! String}
print (arrayValues)

答案 1 :(得分:0)

将json转换为字典数组。

let mainarray = jsondata as? [[String:Any]]

答案 2 :(得分:0)

下面是解析json字符串的示例代码

// I've escaped the double quotes so that it can run, you don't need to do it because you already have the string
let responseStr = "[{\"Loc_District\":8119,\"districtname\":\"अजमेर \",\"District_NameEng\":\"AJMER\"},{\"Loc_District\":8104,\"districtname\":\"अलवर \",\"District_NameEng\":\"ALWAR\"},{\"Loc_District\":8125,\"districtname\":\"बांसवाड़ा\",\"District_NameEng\":\"BANSWARA\"},{\"Loc_District\":8128,\"districtname\":\"बारां \",\"District_NameEng\":\"BARAN\"},{\"Loc_District\":8115,\"districtname\":\"बाड़मेर \",\"District_NameEng\":\"BARMER\"},{\"Loc_District\":8105,\"districtname\":\"भरतपुर \",\"District_NameEng\":\"BHARATPUR\"},{\"Loc_District\":8122,\"districtname\":\"भीलवाडा \",\"District_NameEng\":\"BHILWARA\"},{\"Loc_District\":8101,\"districtname\":\"बीकानेर \",\"District_NameEng\":\"BIKANER\"},{\"Loc_District\":8121,\"districtname\":\"बून्दी \",\"District_NameEng\":\"BUNDI\"},{\"Loc_District\":8126,\"districtname\":\"चित्तौड़गढ़ \",\"District_NameEng\":\"CHITTORGARH\"},{\"Loc_District\":8102,\"districtname\":\"चूरू \",\"District_NameEng\":\"CHURU\"},{\"Loc_District\":8109,\"districtname\":\"दौसा \",\"District_NameEng\":\"DAUSA\"},{\"Loc_District\":8106,\"districtname\":\"धौलपुर \",\"District_NameEng\":\"DHOLPUR\"},{\"Loc_District\":8124,\"districtname\":\"डूंगरपुर \",\"District_NameEng\":\"DUNGARPUR\"},{\"Loc_District\":8099,\"districtname\":\"गंगानगर \",\"District_NameEng\":\"GANGANAGAR\"},{\"Loc_District\":8100,\"districtname\":\"हनुमानगढ \",\"District_NameEng\":\"HANUMANGARH\"},{\"Loc_District\":8110,\"districtname\":\"जयपुर \",\"District_NameEng\":\"JAIPUR\"},{\"Loc_District\":8114,\"districtname\":\"जैसलमेर \",\"District_NameEng\":\"JAISALMER\"},{\"Loc_District\":8116,\"districtname\":\"जालोर \",\"District_NameEng\":\"JALORE\"},{\"Loc_District\":8129,\"districtname\":\"झालावाड \",\"District_NameEng\":\"JHALAWAR\"},{\"Loc_District\":8103,\"districtname\":\"झुंझुंनु \",\"District_NameEng\":\"JHUNJHUNU\"},{\"Loc_District\":8113,\"districtname\":\"जोधपुर \",\"District_NameEng\":\"JODHPUR\"},{\"Loc_District\":8107,\"districtname\":\"करौली \",\"District_NameEng\":\"KAROULI\"},{\"Loc_District\":8127,\"districtname\":\"कोटा \",\"District_NameEng\":\"KOTA\"},{\"Loc_District\":8112,\"districtname\":\"नागौर \",\"District_NameEng\":\"NAGAUR\"},{\"Loc_District\":8118,\"districtname\":\"पाली \",\"District_NameEng\":\"PALI\"},{\"Loc_District\":8131,\"districtname\":\"प्रतापगढ \",\"District_NameEng\":\"PRATAPGARH\"},{\"Loc_District\":8123,\"districtname\":\"राजसमन्द \",\"District_NameEng\":\"RAJSAMAND\"},{\"Loc_District\":8108,\"districtname\":\"सवाई माधोपुर \",\"District_NameEng\":\"SAWAI MADHOPUR\"},{\"Loc_District\":8111,\"districtname\":\"सीकर \",\"District_NameEng\":\"SIKAR\"},{\"Loc_District\":8117,\"districtname\":\"सिरोही \",\"District_NameEng\":\"SIROHI\"},{\"Loc_District\":8120,\"districtname\":\"टोंक \",\"District_NameEng\":\"TONK\"},{\"Loc_District\":8130,\"districtname\":\"उदयपुर \",\"District_NameEng\":\"UDAIPUR\"}]"


// Convert your response string to data or if you've data then pass it directly
let jsonData = responseStr.data(using: .utf8)

do {
  let json = try JSONSerialization.jsonObject(with: jsonData!, options: [])
  if let array = json as? [[String : AnyObject]] {
    for obj in array {
      let distName = obj["districtname"]
      print("\(String(describing: distName!))")
    }
  }
}
catch {
  print("Couldn't parse json \(error)")
}