簡體   English   中英

如何在沒有任何第三方庫的情況下在Swift 3.0中使用Alamofire解析JSON

[英]How to parse JSON using Alamofire in Swift 3.0 without any third-party library

在這里,我想通過url解析JSON。 這是url上可用的實際JSON數據。 因此,我需要解析它並使用Alamofire讀取我的應用程序。 但是我做不到。

網址中的JSON數據。

  {
        "main": [
        {
        "date": "2017-01-11",
        "USDARS": "15.8302",
        "USDCLP": "670.400024",
        "USDSDG": "6.407695"
        },
        {
        "date": "2017-01-12",
        "USDARS": "15.804999",
        "USDCLP": "661.599976",
        "USDSDG": "6.407697"
        },
        {
        "date": "2017-01-13",
        "USDARS": "15.839041",
        "USDCLP": "659.200012",
        "USDSDG": "6.407704"
        },
        {
        "date": "2017-01-14",
        "USDARS": "15.839041",
        "USDCLP": "659.200012",
        "USDSDG": "6.407704"
        }
    ]
}

如何在Swift 3.0中使用Alamofire閱讀

下面是我實際上試圖通過url解析JSON數據的內容。

Alamofire.request("myurl") .responseJSON { response in

            print("In Alamofire")
            if let arr = response.result.value as? [String:AnyObject]
            {

                if let arr = response.result.value as? [NSDictionary]
                {
                    let val1 = (arr["main"]["USDARS"] as? String)
                    print(val1)
                    //It does not print any thing.
                }
            }
        }

請幫我。 我是新來的。

頂級json是[String:Any] ,main是一個數組,即[[String:String]]

    Alamofire.request("myurl") .responseJSON { response in
        if let result = response.result.value as? [String:Any], 
           let main = result["main"] as? [[String:String]]{
           // main[0]["USDARS"] or use main.first?["USDARS"] for first index or loop through array
           for obj in main{
               print(obj["USDARS"])
               print(obj["date"])
           }
        }
    }

您應該使用SwiftyJson ,這是一個用於json解析的庫,對於Alamofire非常有用

在您的情況下,您可以使用swiftyJson執行以下操作:

//Array & Dictionary
var jsonArray: JSON =  [
   "main": ["date": "2017-01-11", "USDARS": "15.8302"]
]

let dateString = jsonArray["main"][0]["date"].string

print(dateString) = "2017-01-11"
 @IBAction func btnget(_ sender: UIButton) {

    let urlpath : String = "http://202.131.123.211/UdgamApi_v4/App_Services/UdgamService.asmx/GetAllTeacherData?StudentId=2011111"

    let url = URL(string: urlpath)

    var urlrequest = URLRequest(url: url!)

    urlrequest.httpMethod = "GET"

    let config = URLSessionConfiguration.default
    let session = URLSession(configuration: config)

    let task = session.dataTask(with: urlrequest) { (data, response, error) in

        do {
            guard let getResponseDic = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: AnyObject] else {
                print("error trying to convert data to JSON")
                return
            }
            // now we have the todo, let's just print it to prove we can access it

            print(getResponseDic as NSDictionary)

            let dic = getResponseDic as NSDictionary
            let msg = dic.object(forKey: "message")

            print(dic)
            //let arrObj = dic.object(forKey: "teacherDetail") as! NSArray
            //print(arrObj)

            //let arr = dic.value(forKey: "TeacherName")as! NSArray

            print(((dic.value(forKey: "teacherDetail") as! NSArray).object(at: 0) as! NSDictionary).value(forKey: "TeacherName") as! String)

            // the todo object is a dictionary
            // so we just access the title using the "title" key
            // so check for a title and print it if we have one

            // print("The title is: " + todoTitle)
        } catch  {
            print("error trying to convert data to JSON")
            return
        }
    } task.resume()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM