json - 无法使用 Alamofire 从 Rest api 选择 json 数组

标签 json swift alamofire

我是 swift 新手,我正在尝试使用 Alamofire 从 json 文件获取链接。我已经在谷歌搜索了几天,但没有找到任何有用的解决方案。我不太擅长 json 数组。 我需要来自(_links.https://api.w.org/featuredmedia.href)的链接,这是我的代码:

guard let ide = self.json[0]["_links"]["https:\/\/api.w.org\/featuredmedia"]["href"].string else{
        print("Request failed with error")
        return
}
    print(ide)


    Alamofire.request(.GET, "http://tricitychurchofchrist.com/wp-json/wp/v2/ctc_sermon/5640").responseJSON { response in
            guard let _ = response.result.value else{
                print("Request failed with error")
                return
            }
    }

这是 json:

{
"id": 5854,
"date": "2016-01-17T20:22:28",
"date_gmt": "2016-01-17T20:22:28",
"guid": {
    "rendered": "http:\/\/tricitychurchofchrist.com\/?post_type=ctc_sermon&p=5854"
},
"modified": "2016-01-18T19:16:10",
"modified_gmt": "2016-01-18T19:16:10",
"slug": "inviting-the-lightaudio-upload",
"type": "ctc_sermon",
"link": "http:\/\/tricitychurchofchrist.com\/sermons\/inviting-the-lightaudio-upload\/",
"title": {
    "rendered": "Inviting The Light"
},
"content": {
    "rendered": ""
},
"excerpt": {
    "rendered": ""
},
"author": 1,
"featured_image": 2007,
"comment_status": "closed",
"ping_status": "closed",
"_links": {
    "self": [{
        "href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/ctc_sermon\/5854"
    }],
    "collection": [{
        "href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/ctc_sermon"
    }],
    "about": [{
        "href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/types\/ctc_sermon"
    }],
    "author": [{
        "embeddable": true,
        "href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/users\/1"
    }],
    "replies": [{
        "embeddable": true,
        "href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/comments?post=5854"
    }],
    "version-history": [{
        "href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/ctc_sermon\/5854\/revisions"
    }],
    "https:\/\/api.w.org\/featuredmedia": [{
        "embeddable": true,
        "href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/media\/2007"
    }],
    "https:\/\/api.w.org\/attachment": [{
        "href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/media?parent=5854"
    }],
    "https:\/\/api.w.org\/term": [{
        "taxonomy": "ctc_sermon_topic",
        "embeddable": true,
        "href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/ctc_sermon\/5854\/ctc_sermon_topic"
    }, {
        "taxonomy": "ctc_sermon_book",
        "embeddable": true,
        "href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/ctc_sermon\/5854\/ctc_sermon_book"
    }, {
        "taxonomy": "ctc_sermon_series",
        "embeddable": true,
        "href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/ctc_sermon\/5854\/ctc_sermon_series"
    }, {
        "taxonomy": "ctc_sermon_speaker",
        "embeddable": true,
        "href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/ctc_sermon\/5854\/ctc_sermon_speaker"
    }, {
        "taxonomy": "ctc_sermon_tag",
        "embeddable": true,
        "href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/v2\/ctc_sermon\/5854\/ctc_sermon_tag"
    }]
}}

我正在使用来自 WordPress 站点的 json 的 WP REST API。

最佳答案

您返回的数据类型是对象类型,需要将其更改为JSON对象,可以使用SwiftJSON github上开源,代码如下:

    Alamofire.request(.GET, url, parameters: ["foo": "bar"])
        .response { request, response, data, error in
            let jsonval = JSON(data:data!)
            let jsonDic:NSDictionary = jsonval.object as! NSDictionary

    }

jsonDic现在是NSDictionary类型,将NSDictionary改为NSArray,就可以直接使用了,希望有帮助

关于json - 无法使用 Alamofire 从 Rest api 选择 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34872007/

相关文章:

json - Swift 4 Decodable - 带有对象的数组

javascript - 在 react 中用复选框填充Json表并根据JSON数据检查它们

swift - 测试 UIAlertController 是否已经出现

iOS Swift - 有没有办法将 subview 添加到表格单元格中并正确定位它?

ios - 为什么文本标签是空的,而调试器说它不是? swift 应用程序?

python - pandas read_json 错误地将大整数读取为字符串

php - cURL PHP RESTful 服务总是返回 FALSE

swift - cocoa应用程序中关于串口获取操作不允许的Xcode C++代码

json - 无法将类型 'DataResponse<Any>' 的值转换为预期的参数类型 'Data'

ios - 如何在 Swift 4 中将条件实现到 UITableViewDataSource