lantianyiming 2013-08-05 08:44 采纳率: 0%
浏览 2917
已采纳

如何访问NSDictionary的内容

在访问字典内容的时候失败:

NSLog(@"self.userCommentsArray %@",self.userCommentsArray);返回null。

请帮忙:

NSData *jsonData = [NSData dataWithContentsOfURL:myURL];
NSDictionary *userCommentsDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
    NSLog(@"userCommentsDictionary %@",userCommentsDictionary); // this works
    self.userCommentsArray = [[userCommentsDictionary objectForKey:@"from"] objectForKey:@"name"];
    NSLog(@"self.userCommentsArray %@",self.userCommentsArray); 

这是dicitionary的NSlog输出:

userCommentsDictionary {
        data =     (
                    {
                created = "2013-07-16T18:42:56+02:00";
                from =             {
                    id = 27;
                    name = "user-4";
                };
                id = 2553;
                message = "liquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
            },
                    {
                created = "2013-07-16T18:42:56+02:00";
                from =             {
                    id = 28;
                    name = "user-5";
                };
                id = 2554;
                message = "x ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
            },
                    {
                created = "2013-07-19T16:18:56+02:00";
                from =             {
                    id = 24;
                    name = "user-1";
                };
                id = 5125;
                message = test comment;
            },
                    {
                created = "2013-07-19T17:00:21+02:00";
                from =             {
                    id = 24;
                    name = "user-1";
                };
                id = 5126;
                message = "test comment ";
       }
        );
        meta =     {
            totalCount = 18;
        };
    }
  • 写回答

1条回答

  • ReyZhang 移动开发领域新星创作者 2013-08-05 09:00
    关注

    userCommentsDictionary 中可以根据data得到一组数据。你要找的内容就在这一组数据中

    NSArray *data=userCommentsDictionary[@"data"];
    

    这时返回的是一个数组,你可以根据下标索引取任意一行中的数据,也可以循环遍历所有的数据。下面的代码取的是第一行的数据

     id entity=[data objectAtIndex:0];
    

    再取对象中的from下的name

    NSString *name=entity[@"from"][@"name"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决