我是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。
答案 0 :(得分:0)
您返回的数据类型是对象类型,您应该将其更改为JSON对象,您可以在github上使用SwiftJSON开源,并且代码如下:
wp_render_posts
jsonDic现在是一个NSDictionary类型,将NSDictionary更改为NSArray,你可以直接使用它,希望有帮助