gpt4 book ai didi

ios - 使用 Swift 使用 JSON

转载 作者:行者123 更新时间:2023-11-28 13:14:30 32 4
gpt4 key购买 nike

我正在实现代码以使用 JSON。

   func setup() {

if let flickrURL = NSURL(string: "https://api.flickr.com/services/feeds/photos_public.gne?tags=texas&tagmode=any&format=json") {

NSURLSession.sharedSession().dataTaskWithURL(flickrURL,
completionHandler: { (data :NSData!, response: NSURLResponse!, error: NSError!) -> Void in

let result = NSString(data: data, encoding: NSUTF8StringEncoding)

var error:NSError? = nil

if let jsobObject :AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error:&error) {

println("\(error?.description)")

}

println("\(result)")

}).resume()
}
}

我经常收到以下错误:

Optional("Error Domain=NSCocoaErrorDomain Code=3840 \"The operation couldn’t be completed. (Cocoa error 3840.)\" (Invalid value around character 0.) UserInfo=0x7ff4a2d1bdc0 {NSDebugDescription=Invalid value around character 0.}")

我在不同的网站上验证了 json,有些地方说有效,有些地方说无效。我很困惑!

这是无效的结果 JSON。

jsonFlickrFeed({
"title": "Recent Uploads tagged texas",
"link": "https://www.flickr.com/photos/tags/texas/",
"description": "",
"modified": "2015-04-11T21:32:21Z",
"generator": "https://www.flickr.com/",
"items": [
{
"title": "Muleshoe Bend Bluebonnets - 132 HDR",
"link": "https://www.flickr.com/photos/ericmorton/17087559606/",
"media": {
"m": "https://farm9.staticflickr.com/8786/17087559606_c6c3108eae_m.jpg"
},
"date_taken": "2015-04-11T13:16:36-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/ericmorton/\">ericmorton<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/ericmorton/17087559606/\" title=\"Muleshoe Bend Bluebonnets - 132 HDR\"><img src=\"https://farm9.staticflickr.com/8786/17087559606_c6c3108eae_m.jpg\" width=\"240\" height=\"160\" alt=\"Muleshoe Bend Bluebonnets - 132 HDR\" /><\/a><\/p> ",
"published": "2015-04-11T21:32:21Z",
"author": "nobody@flickr.com (ericmorton)",
"author_id": "31408561@N00",
"tags": "texas hdr bluebonnets muleshoebend"
},
{
"title": "Muleshoe Bend Bluebonnets - 095 HDR",
"link": "https://www.flickr.com/photos/ericmorton/16493362113/",
"media": {
"m": "https://farm9.staticflickr.com/8805/16493362113_0b6910ccb2_m.jpg"
},
"date_taken": "2015-04-11T13:09:22-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/ericmorton/\">ericmorton<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/ericmorton/16493362113/\" title=\"Muleshoe Bend Bluebonnets - 095 HDR\"><img src=\"https://farm9.staticflickr.com/8805/16493362113_0b6910ccb2_m.jpg\" width=\"240\" height=\"160\" alt=\"Muleshoe Bend Bluebonnets - 095 HDR\" /><\/a><\/p> ",
"published": "2015-04-11T21:30:53Z",
"author": "nobody@flickr.com (ericmorton)",
"author_id": "31408561@N00",
"tags": "texas hdr bluebonnets muleshoebend"
},
{
"title": "Muleshoe Bend Bluebonnets - 105 HDR",
"link": "https://www.flickr.com/photos/ericmorton/16925716248/",
"media": {
"m": "https://farm9.staticflickr.com/8749/16925716248_9f2dcdf265_m.jpg"
},
"date_taken": "2015-04-11T13:12:41-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/ericmorton/\">ericmorton<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/ericmorton/16925716248/\" title=\"Muleshoe Bend Bluebonnets - 105 HDR\"><img src=\"https://farm9.staticflickr.com/8749/16925716248_9f2dcdf265_m.jpg\" width=\"240\" height=\"165\" alt=\"Muleshoe Bend Bluebonnets - 105 HDR\" /><\/a><\/p> ",
"published": "2015-04-11T21:31:10Z",
"author": "nobody@flickr.com (ericmorton)",
"author_id": "31408561@N00",
"tags": "texas hdr bluebonnets muleshoebend"
},
{
"title": "Muleshoe Bend Bluebonnets - 090 HDR",
"link": "https://www.flickr.com/photos/ericmorton/16906114977/",
"media": {
"m": "https://farm8.staticflickr.com/7722/16906114977_7e369312cb_m.jpg"
},
"date_taken": "2015-04-11T13:08:56-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/ericmorton/\">ericmorton<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/ericmorton/16906114977/\" title=\"Muleshoe Bend Bluebonnets - 090 HDR\"><img src=\"https://farm8.staticflickr.com/7722/16906114977_7e369312cb_m.jpg\" width=\"240\" height=\"157\" alt=\"Muleshoe Bend Bluebonnets - 090 HDR\" /><\/a><\/p> ",
"published": "2015-04-11T21:30:36Z",
"author": "nobody@flickr.com (ericmorton)",
"author_id": "31408561@N00",
"tags": "texas hdr bluebonnets muleshoebend"
},
{
"title": "Muleshoe Bend Bluebonnets - 127 HDR",
"link": "https://www.flickr.com/photos/ericmorton/16927325659/",
"media": {
"m": "https://farm8.staticflickr.com/7587/16927325659_48616d9bc7_m.jpg"
},
"date_taken": "2015-04-11T13:13:46-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/ericmorton/\">ericmorton<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/ericmorton/16927325659/\" title=\"Muleshoe Bend Bluebonnets - 127 HDR\"><img src=\"https://farm8.staticflickr.com/7587/16927325659_48616d9bc7_m.jpg\" width=\"240\" height=\"159\" alt=\"Muleshoe Bend Bluebonnets - 127 HDR\" /><\/a><\/p> ",
"published": "2015-04-11T21:32:00Z",
"author": "nobody@flickr.com (ericmorton)",
"author_id": "31408561@N00",
"tags": "texas hdr bluebonnets muleshoebend"
},
{
"title": "DSC00435",
"link": "https://www.flickr.com/photos/laura_rivera/16906113517/",
"media": {
"m": "https://farm9.staticflickr.com/8737/16906113517_4a9c6c7929_m.jpg"
},
"date_taken": "2015-04-11T13:17:10-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/laura_rivera/\">laura_rivera<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/laura_rivera/16906113517/\" title=\"DSC00435\"><img src=\"https://farm9.staticflickr.com/8737/16906113517_4a9c6c7929_m.jpg\" width=\"240\" height=\"160\" alt=\"DSC00435\" /><\/a><\/p> ",
"published": "2015-04-11T21:30:26Z",
"author": "nobody@flickr.com (laura_rivera)",
"author_id": "20516990@N04",
"tags": "texas bluebonnet wildflowers wildflower bluebonnets laurarivera"
},

]
})
]

如何让它生效?

最佳答案

您需要根据文档(本页底部)将 nojsoncallback=1 添加到您的 URL 的末尾:

https://www.flickr.com/services/api/response.json.html

否则,API返回JSONP,回调函数jsonFlickrApi

关于ios - 使用 Swift 使用 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29583377/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com