简体   繁体   English

从JSON端点检索数据

[英]Retrieving Data from JSON Endpoint

I'm using the Drupal iOS SDK to retrieve data from an entity (Comments) using the following code in my ViewController: 我正在使用Drupal iOS SDK在ViewController中使用以下代码从实体(注释)检索数据:

ViewController.h ViewController.h

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

ViewController.m 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"); }
     ];

The JSON data is returned and displayed in my console like so: 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.";
                }
            );
        };
) 

I'm trying to display what's returned from the "comment_body" in a UILabel (the XML at the endpoint looks like this): 我试图在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>

My question: What should the code look like in order to retrieve that line? 我的问题:要检索该行,代码应该是什么样? I'm thinking it should look something like... 我认为它应该看起来像...

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

But comments is an NSMutableArray (and thus, that line won't work). 但是注释是一个NSMutableArray(因此该行将不起作用)。 Any help is appreciated; 任何帮助表示赞赏; thank you! 谢谢!

尝试这个

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

Use a NSDictionary instead, like this: 改为使用NSDictionary,如下所示:

 { 
        NSDictionary *jsonDictionary= [NSDictionary alloc]init]
             jsonDictionary= response;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM