簡體   English   中英

解析AlamoFire的JSON輸出

[英]Parse JSON output from AlamoFire

我正在使用最新的Alamofire來管理對服務器的GET http請求。 我正在使用以下內容來GET和解析JSON:

 Alamofire.request(.GET, "*******")
            .responseJSON {(request, response, JSON, error) in

                if let statusesArray = JSON as? NSArray{
                    if let aStatus = statusesArray[0] as? NSDictionary{

                        //OUTPUT SHOWN BELOW
                        println( "BREAKING DOWN \(aStatus)")

                        // This doesn't output anything?????
                        if let user = aStatus["title"] as? NSDictionary{
                            println( "TITLE \(user)")
                        }


                    }
                }

                println(JSON)

        }

GET請求有效,而我面臨的問題是嘗試在JSON輸出中定位標題鍵。 println不輸出任何內容。 我可以輕松地在目標C中執行此操作,但是由於某些原因,SWIFT使我感到困惑。 println的輸出

BREAKING DOWN {
    "__v" = 0;
    "_id" = 5588468c65340938150f674f;
    endDate = 1435004396596;
    fullDescription = "Neutra blog McSweeney's pug Austin, put a bird on it fanny pack. Try-hard jean shorts trust fund vinyl kale chips, blog distillery pickled synth. Tofu fap Intelligentsia umami, McSweeney's pork belly church-key literally roof party crucifix lumbersexual meditation irony four loko. Mlkshk tousled before they sold out pork belly. PBR&B craft beer Tumblr, trust fund swag chillwave Truffaut.Retro cray narwhal 3 wolf moon. Pug master cleanse dreamcatcher, Vice Blue Bottle next level Helvetica messenger bag distillery chillwave pickled tattooed wayfarers DIY cold-pressed.";
    shortDescription = "Neutra blog McSweeney's pug Austin, put a bird on it fanny pack.";
    startDate = 1434994316596;
    title = "Artisanal Yoghurt";
}

如果我相信aStatus的println,則屬性title是String而不是Dictionary。

更改代碼中的這一部分(以字符串形式而不是NSDictionary形式):

if let user = aStatus["title"] as? String {
    println( "TITLE \(user)")
}

暫無
暫無

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

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