ios - 从 Facebook JSON 访问好友数据

标签 ios json swift facebook

我是 Swift 和 JSON 的新手,需要一些快速帮助。我的应用程序有 Facebook 身份验证,并且会读取已授权该应用程序的 friend 列表。一切都正确记录,但我不熟悉如何打印每个 friend 的 friend ID。这是我的代码:

FBSDKGraphRequest(graphPath: "me",
                 parameters: ["fields": "id, name,friends, first_name, last_name, picture.type(large), email"])
                .start(completionHandler: { (connection, result , error) -> Void in
                    let data:[String:AnyObject] = result as! [String : AnyObject]
                    print(data)

这是它为 friend 记录的数据:

"friends": {
    data = (
        {
            id = 10104542348623278;
            name = "John Smith";
        },

但我不知道如何打印每个 friend 的 ID。这是我试过不成功的。

for friend in data["friends"] {
    let friendId = data["friends"]["id"]
    print(friendId)
}

任何帮助将不胜感激。

最佳答案

  • friends 是一个字典。
  • data里面是一个Dictionary数组。

    if let friendsData = data["friends"] as? [String:AnyObject] {
    
        if let friends = friendsData["data"] as? [[String:AnyObject]] {
            for friend in friends{
                print(friend["id"])
            }
        }
    }
    

关于ios - 从 Facebook JSON 访问好友数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39902902/

相关文章:

javascript - 带有 HTML 标签的字符串在 foreach 中无法被识别

c++ - 带有 restclient-cpp 的 JSON 属性树

ios - 不知道这是什么意思 Thread 1 : EXC_BREAKPOINT(code=EXC_1386_BPT, subcode=0x0)

ios - 如何快速在 UICollectionView 中添加分页?

ios - 从 UITableView 转发 ControlEvent

ios - 在同一选项卡上的三个 UIButton 之间切换 selectedState

ios - 如何添加 xib View 以查看 Controller swift 3

ios - "Binary operator ' ~= ' cannot be applied to operands of type '

ios - 在任何 UIViewController 之间交替?

java - 使用 Java 正则表达式提取包含 URL 的特定行