ios - 如何遍历 JSON 对象并将其视为字符串 - Swift

标签 ios json swift

我刚开始使用 swift 进行编码,现在我可以从 JSON 中获取单个值,但我似乎无法通过遍历数组从中获取所有值。 所以我的问题是如何获取所有值并将其视为 float 或字符串。

这是我的代码:

 let url = URL(string: "http://api.fixer.io/latest")

    let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
        if error != nil
        {
            print ("ERROR")
        }
        else
        {
            if let content = data
            {
                do
                {
                    //Array
                    let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
                    //print(myJson)

                    for items in myJson [AnyObject] {
                        print(items)
                    }

                    //here is the single value part, it looks for the rates then it puts it in label.

                    if let  rates = myJson["rates"] as? NSDictionary{


                        if let currency = rates["AUD"]{


                        print(currency);

                       self.label.text=String(describing: currency)


                        }





                    }

                }

                catch
                {


                }
            }
        }
    }
    task.resume()

最佳答案

试试这段代码:

import UIKit

class ViewController: UIViewController {


override func viewDidLoad() {
    super.viewDidLoad()

    self.getJson()
}

func getJson(){

    let url = URL(string: "http://api.fixer.io/latest")

    let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
        if error != nil
        {
            print ("ERROR")
        }
        else
        {
            if let content = data
            {
                do
                {
                        //Dic
                        guard let myJson:[String:Any] = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String:Any] else {return}
                        //print(myJson)

                        for items in myJson {
                            print(items)
                        }

                        //here is the single value part, it looks for the rates then it puts it in label.

                        if let  rates = myJson["rates"] as? NSDictionary{


                            if let currency = rates["AUD"]{


                                print(currency);

                               // self.label.text=String(describing: currency)


                            }


                        }

                    }

                    catch
                    {


                    }
                }
            }
        }
        task.resume()
    }
}

控制台的结果如下:
enter image description here

myJson 就是你想要的字典。

关于ios - 如何遍历 JSON 对象并将其视为字符串 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41667637/

相关文章:

swift - 使用 Alamofire 调用带有 HTTP header 和正文的 API

ios - 奇怪的iOS应用在模拟器上崩溃

iPhone Lite版本升级

python - 额外数据: line 2 column 1 - line 341211 -- Error in json.加载

swift - 图像分类器在 Xcode 中的分类标签中不显示结果

ios - (NSObject, AnyObject) 不可转换为 DictionaryIndex<NSObject, AnyObject>

ios - 特定用户信息的 Firebase 查询不起作用。我该如何解决这个问题?

ios - 自动水平滚动

javascript - 简单的 react 过滤

android - 在 App Inventor 2 中使用 ResultNotifier.Show Progress Dialog 的正确方法?