如何从 swift 3 facebook 图形请求中解析这个 JSON
Posted
技术标签:
【中文标题】如何从 swift 3 facebook 图形请求中解析这个 JSON【英文标题】:How to parse this JSON from swift 3 facebook graph request 【发布时间】:2016-09-27 05:57:13 【问题描述】:我正在使用图形请求来获取使用此代码的 json:
nextrequest.start( (response: HTTPURLResponse?, result: Any?) in
print(result)
)
这是下面的jsonresult
,我不知道如何访问里面的数据,比如性别、id和名字……
Optional(FacebookCore.GraphRequestResult<FacebookCore.GraphRequest>.success(FacebookCore.GraphResponse(rawResponse: Optional(
gender = male;
id = 1128614937219535;
name = "Rayan Slim";
picture =
data =
height = 320;
"is_silhouette" = 0;
url = "https://scontent.xx.fbcdn.net/v/t1.0-1/p320x320/12541113_961418627272501_5451131278168499090_n.jpg?oh=47433bc236ce63ce1c07b92499087f29&oe=586A406A";
width = 320;
;
;
))))
任何帮助将不胜感激!!!!
【问题讨论】:
【参考方案1】:在获得许可后,这里的功能就起作用了。
if AccessToken.current != nil
GraphRequest(graphpath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start( (urlResponse, requestResult) in
switch requestResult
case .Success(let response):
if let responseDictionary = response.dictionaryValue
let email = responseDictionary["email"] as? String
print(email)
let first = responseDictionary["name"] as? String
print(first)
if let picture = responseDictionary["picture"] as? NSDictionary
if let data = picture["data"] as? NSDictionary
if let profilePicture = data["url"] as? String
print(profilePicture)
case .Failed(let error):
print(error)
)
【讨论】:
我假设此方法已被弃用,因为截至 2021 年 3 月,没有成功或失败的枚举。它失败并出现错误“Enum case 'success' not found in type 'Any?'”【参考方案2】:这是在 Swift 3 中使用 Facebook SDK for Swift 测试和运行的
let pictureRequest = GraphRequest(graphPath: "me/picture?type=large&redirect=false", parameters: [:])
pictureRequest.start
(urlResponse, requestResult) in
switch requestResult
case .failed(let error):
print("error in graph request:", error)
break
case .success(let graphResponse):
if let responseDictionary = graphResponse.dictionaryValue
print(responseDictionary)
var dict: NSDictionary!
dict = responseDictionary["data"] as! NSDictionary
print(dict)
print(dict["url"])
【讨论】:
在展开可选值时意外发现 nil【参考方案3】:if let userDataDict = result as? NSDictionary
self.first_name = userDataDict["first_name"] as? String
self.id = userDataDict["id"] as? String
self.last_name = userDataDict["last_name"] as? String
let pictDict = userDataDict["picture"] as? NSDictionary
let pictureUrl = pictDict?["data"] as? NSDictionary
self.pictureUrl = pictureUrl?["url"] as? String
【讨论】:
【参考方案4】:喜欢
nextrequest.start( (response: HTTPURLResponse?, result: Any?) in
print(result)
if let userData = result as? [NSObject: Any]
if let name = userData["name"] as? String
print(name)
if let picture = userData["picture"] as? [NSObject: Any]
if let data = picture["data"] as? [NSObject: Any]
if let profilePictureURL = data["url"] as? String
// Now add the data to the UI elements
print (profilePictureURL)
)
【讨论】:
这些导致它失败: Optional(FacebookCore.GraphRequestResult使用一些 JSON 解析器,可能是 Unbox,它可以更轻松地处理 JSON。 此代码未经测试,但它是您如何做到这一点的概述。将数据存储在结构中而不是使用字典始终是代码。
typealias JSON = [String: Any]
protocol Model
init?(json: JSON)
func getDataFromFacebook()
...
nextrequest.start
(response: HTTPURLResponse?, result: Any?) in
self.handleFacebookResult(result)
func handleFacebookResult(_ result: Any?)
guard
let json = result as? JSON,
let person = Person(json: json)
else return
//do something with person! :)
struct Person: Model
let name: String
let gender: String
let picture: Picture
init?(json: JSON)
guard
let name = json["name"] as? String,
let gender = json["gender"] as? String,
let pictureJson = json["picture.data"] as? JSON, // "picture.data" possible when using 'Unbox'
let picture = Picture(json: pictureJson)
else return nil
self.name = name
self.gender = gender
self.picture = picture
struct Picture: Model
let height: Int // Or maybe float...?
let width: Int // Or maybe float...?
let url: String
init?(json: JSON)
guard
let height = json["height"] as? Int,
let width = json["width"] as? Int,
let url = json["url"] as? String
else return nil
self.height = height
self.width = width
self.url
【讨论】:
以上是关于如何从 swift 3 facebook 图形请求中解析这个 JSON的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Swift 3 从 Facebook SDK 下载照片?
FBSDKGRAPHRequestConnection 警告 swift 3