我如何从 swift4 中的字符串中获取特定的键值
Posted
技术标签:
【中文标题】我如何从 swift4 中的字符串中获取特定的键值【英文标题】:how do i get particular key value from string in swift4 【发布时间】:2017-11-29 09:43:20 【问题描述】:我有来自 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"]
【问题讨论】:
那不是xml
,那是JSON
。使用JSONDecoder
和map
你试过什么?您当前的方法会导致什么问题?你有什么代码?。
@vadian 你能解释一下吗
【参考方案1】:
它是字典的 JSON 数组,您可以通过使用 map
和下面的数组来获取特定键的所有值。
let arrayValues = mainArray.map$0["District_NameEng"] as! String
print (arrayValues)
【讨论】:
Cannot subscript a value of type 'String' with a index of 'String' 显示此错误 我认为您将目标 c 和 swift 混合在一起。把你的代码放在你如何获得 mainArray func parser(_ parser: XMLParser, foundCharacters string: String) 表单字符串我得到了我粘贴在我的问题中的所有值。现在告诉我如何将这个字符串更改为数组并获取值,正如你上面所说 首先您需要将 String 转换为 Array以下是解析 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)")
【讨论】:
当我这样做时:让 responseStr = string 然后出现此错误 JSON 文本没有以数组或对象开头,并且允许未设置片段的选项 你能告诉我你给变量string
赋值的代码
还有print("\(string)")
打印什么?
print("(string)") 这会在控制台上给出响应 >>> ["Loc_District":8119,"districtname":" अजमेर ","District_NameEng":"AJMER", "Loc_District":8104,"districtname":"अलवर ","District_NameEng":"ALWAR"]
@singh Post some code [edit your question],找不到问题【参考方案3】:
将 json 转换为字典数组。
let mainarray = jsondata as? [[String:Any]]
【讨论】:
以上是关于我如何从 swift4 中的字符串中获取特定的键值的主要内容,如果未能解决你的问题,请参考以下文章
如何从另一个模式中获取特定的键值,通过比较它们是不是具有相同的键值?
如何在 Firebase 实时数据库(Android 开发)中获取特定子项的键值?
在 Swift 3.0 中,没有在数组中的索引处获取对象的键值语法