【问题标题】:extracting information from NSDictionary从 NSDictionary 中提取信息
【发布时间】:2012-08-02 20:23:16
【问题描述】:

我有一个来自 Flickr api 请求的 NSDictionary 响应,它的描述如下所示:

self.userInfo: {
"_text" = "\n\n";
person =     {
    "_text" = "\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n";
    description =         {
    };
    iconfarm = 0;
    iconserver = 0;
    id = "83943196@N02";
    ispro = 0;
    location =         {
    };
    "mbox_sha1sum" =         {
        "_text" = 7b61c5d24f12345678be82c31234567830540;
    };
    mobileurl =         {
        "_text" = "http://m.flickr.com/photostream.gne?id=12345678";
    };
    nsid = "12345678@N02";
    "path_alias" = "";
    photos =         {
        "_text" = "\n\t\t\n\t\t\n\t\t\n\t\t\n\t";
        count =             {
            "_text" = 2;
        };
        firstdate =             {
            "_text" = 12345678;
        };
        firstdatetaken =             {
            "_text" = "2012-08-01 12:46:38";
        };
        views =             {
            "_text" = 0;
        };
    };
    photosurl =         {
        "_text" = "http://www.flickr.com/photos/12345678@N02/";
    };
    profileurl =         {
        "_text" = "http://www.flickr.com/people/12345678@N02/";
    };
    realname =         {
        "_text" = "Me TheUser";
    };
    timezone =         {
        label = "Pacific Time (US & Canada); Tijuana";
        offset = "-08:00";
    };
    username =         {
        "_text" = metheuser;
    };
};
stat = ok;
}

我很难弄清楚如何将实名值提取到 NSString 中。

我试过了:

NSString * temp = [self.userInfo valueForKey:@"realname"];
NSLog (@"FL: nameOfSignedInUser. nameself.userInfo: %@", temp.debugDescription);

但返回为零。

谢谢!

【问题讨论】:

  • 看起来realname 对象嵌套在person 对象中,您是否尝试过将person 作为字典检索,然后从中获取realname
  • 是的,谢谢你的提示。我检查了计数和它的 3。我猜第一个是测试,第二个是人,第三个是统计
  • 如有疑问,请遍历所有根对象的键和对象以查看您拥有的内容。在处理从 XML 解析的任意 Web 服务返回值时,我使用了类似的过程

标签: objective-c ios nsdictionary


【解决方案1】:
NSString *name = [self.userInfo valueForKeyPath:@"person.realname._text"];

【讨论】:

    【解决方案2】:
    NSString *realName = [[[self.userInfo objectForKey:@"person"] objectForKey:@"realname"] objectForKey:@"_text"];
    

    【讨论】:

    • 达米特;我敢肯定,当我回答我的答案时,您的答案看起来不像那样(这是相同的!)。我会删除我的...
    • @trojanfoe 抱歉,我删除并更改了它。没看到别人发的EJV 在这里确实是正确的答案。我从未使用过valueForKeyPath: 很高兴知道。
    • 是的,我什至在 NSArray 类参考文档中都没有看到它。仅在 NSKeyValueCoding 协议参考中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 2017-06-07
    • 2012-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多