尝试使用swiftyJSON在swift中解析JSON(Trying to parse JSON in swift with swiftyJSON)

编程入门 行业动态 更新时间:2024-10-14 20:21:21
尝试使用swiftyJSON在swift中解析JSON(Trying to parse JSON in swift with swiftyJSON)

问题是我无法用我认为用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) }

更多推荐

本文发布于:2023-08-05 20:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437715.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:swift   swiftyJSON   parse   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!