我正在实现使用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"
},
]
})
]
如何使其有效?
答案 0 :(得分:3)
您需要根据文档(本页底部)将nojsoncallback=1
添加到您网址的末尾:
https://www.flickr.com/services/api/response.json.html
否则,API会使用回调函数JSONP
jsonFlickrApi
答案 1 :(得分:-1)
请尝试此操作,它会完全返回您要查找的JSON-
https://api.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1