【问题标题】:Cannot select json array from rest api using Alamofire无法使用 Alamofire 从 rest api 中选择 json 数组
【发布时间】:2016-01-19 08:39:18
【问题描述】:

我是 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 不是一个数组,而是一个对象。你不应该通过索引访问它(json[0]["_links"]),试试json["_links"]...
  • 但是我的 json 中有一个数组正确 "self": [{ "href": "http:\/\/tricitychurchofchrist.com\/wp-json\/wp\/ v2\/ctc_sermon\/5854" }] 以及如何访问这个 "_links": { "self": [{ "href": "http:\/\/tricitychurchofchrist.com\/ wp-json\/wp\/v2\/ctc_sermon\/5854" }]

标签: json swift alamofire


【解决方案1】:

你返回的数据类型是对象类型,你应该把它改成JSON对象,你可以使用github上的SwiftJSON开源,代码如下:

    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,就可以直接使用了,希望帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-27
    • 2018-11-08
    • 2018-02-06
    • 1970-01-01
    • 2019-12-29
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多