【问题标题】:Parse JSON output from AlamoFire解析来自 AlamoFire 的 JSON 输出
【发布时间】:2015-06-22 21:26:10
【问题描述】:

我正在使用最新的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";
}

【问题讨论】:

    标签: json swift nsurlrequest


    【解决方案1】:

    如果我相信aStatus 的println,属性title 是字符串,而不是字典。

    在您的代码中更改此部分(转换为字符串而不是 NSDictionary):

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

    【讨论】:

    • 啊,这是一个愚蠢的错误。谢谢。当它允许我时我会接受
    猜你喜欢
    • 2016-10-01
    • 2022-07-21
    • 1970-01-01
    • 2015-08-05
    • 2016-02-24
    • 2018-04-20
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    相关资源
    最近更新 更多