Brittany
Brittany

Reputation: 1449

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:

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

The JSON data is returned and displayed in my console like so:

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):

<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). Any help is appreciated; thank you!

Upvotes: 1

Views: 51

Answers (2)

Arpit Dongre
Arpit Dongre

Reputation: 1713

Use a NSDictionary instead, like this:

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

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

Upvotes: 0

Avijit Nagare
Avijit Nagare

Reputation: 8802

Try This

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

Upvotes: 1

Related Questions