问题是我无法用我认为用swiftyJSON解析JSON文件的正确代码来填充数组。
我不确定发送请求的过程是否正确。
JSON格式:这应该简化为它真正代表的内容:带有“字符串”键和字典数组值的字典。 然后是一个字符串值的字符串键。 然后是一个字符串,其值为我需要的字符串。
{ "string1" : [ { "string2" : { "string3" : "DataINeed" } } ] }我的代码
func downloadSecondJSONData(completed: DownloadComplete) { let API_ServerKey = "My API Key" let URL_Search = "URL Address" let urlString = "\(URL_Search)" let url = NSURL(string: urlString)! Alamofire.request(.GET,url).responseJSON { (response) -> Void in switch response.result { case .Success: if let value = response.result.value { let json = JSON(value) if let data = json["string1"]["string2"]["string3"].string { self.dataArray.append(data) } } case .Failure(let error): print(error) } completed() } } func printData() { print(self.dataArray.count) }我是如何尝试调用这些方法的
downloadFirstJSONData { () -> () in self.randomMethod() //data i use with the downloadFirstJSONData is required to use in the downloadSecondJSONData self.downloadSecondJSONData({ () -> () in self.printData() //check if data has been stored }) }The issue is i am not able to populate an array with what I thought would be correct code to parse a JSON file with swiftyJSON.
As well as I am not sure if the process at which i send the request is correct.
JSON format: this should be simplified to what it truly represents: A dictionary with a "string" key and value of an array of dictionaries. then a string key with a value of a dictionary. then a string with a value of a string which i need.
{ "string1" : [ { "string2" : { "string3" : "DataINeed" } } ] }my code
func downloadSecondJSONData(completed: DownloadComplete) { let API_ServerKey = "My API Key" let URL_Search = "URL Address" let urlString = "\(URL_Search)" let url = NSURL(string: urlString)! Alamofire.request(.GET,url).responseJSON { (response) -> Void in switch response.result { case .Success: if let value = response.result.value { let json = JSON(value) if let data = json["string1"]["string2"]["string3"].string { self.dataArray.append(data) } } case .Failure(let error): print(error) } completed() } } func printData() { print(self.dataArray.count) }How I am attempting to call the methods
downloadFirstJSONData { () -> () in self.randomMethod() //data i use with the downloadFirstJSONData is required to use in the downloadSecondJSONData self.downloadSecondJSONData({ () -> () in self.printData() //check if data has been stored }) }最满意答案
看起来你没有在第一本字典后访问数组。
我想访问第一个对象的安全方法是:
if let array = json["string1"].array, let firstDict = array.first, let data = firstDict["string2"]["string3"].string { self.dataArray.append(data) }但我想用SwiftyJSON你也可以做一些变化:
if let data = json["string1"][0]["string2"]["string3"].string { self.dataArray.append(data) }Looks like you're not accessing the array after the first dictionary.
I guess the safe way to access the first object would be:
if let array = json["string1"].array, let firstDict = array.first, let data = firstDict["string2"]["string3"].string { self.dataArray.append(data) }But I suppose with SwiftyJSON you can also do a variation of what you had:
if let data = json["string1"][0]["string2"]["string3"].string { self.dataArray.append(data) }更多推荐
发布评论