ios - 从 JSON 端点检索数据

标签 ios objective-c json

我正在使用 Drupal iOS SDK 在我的 ViewController 中使用以下代码从实体(评论)检索数据:

ViewController.h

@property (nonatomic, strong) NSMutableArray *comments;
@property (weak, nonatomic) IBOutlet UILabel *userReviews;

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    NSDictionary *entityData = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"1"] forKey:@"uid"];

    [DIOSEntity
     entityGet:entityData
     name:@"entity_comment"
     eid:@"uid"
     success:^(AFHTTPRequestOperation *op, id response) {
         self.comments = [NSMutableArray arrayWithObject:(NSDictionary*)response];


         NSLog(@"This is all of the data from response %@", response);
         dispatch_async(dispatch_get_main_queue(), ^(void){
           //  [self.tableView reloadData];
         });
     }
     failure:^(AFHTTPRequestOperation *op, NSError *err) { NSLog(@"failed to get data"); }
     ];

JSON 数据返回并显示在我的控制台中,如下所示:

2016-01-08 21:53:13.213 app[2382:935044] Comments are as follows (
    {
        changed = 1450564731;
        cid = 1;
        "comment_body" =         {
            und =             (
                                {
                    format = "filtered_html";
                    "safe_value" = "<p>Christina is amazing with pets; I would recommend her to all users.</p>\n";
                    value = "Christina is amazing with pets; I would recommend her to all users.";
                }
            );
        };
) 

我试图在 UILabel 中显示从“comment_body”返回的内容(端点处的 XML 如下所示):

<comment_body>
<und is_array="true">
<item>
<value>
Christina is amazing with pets; I would recommend her to all users.
</value>
<format>filtered_html</format>
<safe_value>
<p>Christina is amazing with pets; I would recommend her to all users.</p>
</safe_value>
</item>
</und>
</comment_body>

我的问题:为了检索该行,代码应该是什么样的?我认为它应该看起来像...

self.userReviews.text = [self.comments objectForKey:@"comment_body"];

但是 comments 是一个 NSMutableArray(因此,该行将不起作用)。任何帮助表示赞赏;谢谢!

最佳答案

试试这个

 self.userReviews.text = [[[[[self.comments objectAtIndex:0] objectForKey:@"comment_body"] objectForKey:@"und"] objectAtIndex:0] objectForKey:@"value"];

关于ios - 从 JSON 端点检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34690380/

相关文章:

iphone - 在 Objective-C 中获取 php 网站响应

java - 如何将 JSON 数组存储在 JSON 数组结果中并显示在 ListView 中?

ios - iPhone - 沿触摸和拖动路径移动对象

iphone - fbDidLogin 的问题从未调用过 iOS

objective-c - 如何知道我们到达了 NSTableView 的最后一行

ruby-on-rails - 通过JSON进行的嵌套模型验证失败

json - elasticsearch simple_query_string 的通配符?

iphone - 在iphone应用程序中从地址簿中获取照片

ios - 方法 'fontWithName(_:size:)' 与 Objective-C 选择器冲突

ios - Xcode 6 中的图标/启动图像表去了哪里?