Reputation: 1567
I am using iOS 5 new feature to parse JSON and I have no idea that why I am not getting any key value pairs. "aStr" (string representation of data) is putting the right JSON on the output window but I am getting nothing in "dicData" and there is no error either.
Any help is greatly appreciated.
This is what I am using
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.macscandal.com/?json=get_post&post_id=436"]];
NSString* aStr;
aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
//NSLog(@"data = %@",aStr);
NSDictionary *dicData = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&error];
//NSLog(@"error = %@",error);
NSString *title = [dicData objectForKey:@"title"];
Upvotes: 2
Views: 2206
Reputation: 9453
Your JSON is formatted this way:
{
"status": "ok",
"post": {
"id": 436,
"type": "post",
"slug": "foxconn-likely-to-get-assembly-contract-for-apple-tv-set",
"url": "http:\/\/www.macscandal.com\/index.php\/2011\/12\/28\/foxconn-likely-to-get-assembly-contract-for-apple-tv-set\/",
"status": "publish",
"title": "Foxconn Likely to get Assembly Contract for Apple TV Set",
...
I haven't used NSJSONSerialization
but just following the natural JSON parsing alg this is how I would try to get it.
NSDictionary *dicData = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&error];
NSDictionary *postData = [dicData objectForKey:@"post"];
NSString *title = [postData objectForKey:@"title"];
EDIT
Just a simple check method:
-(void)check{
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.macscandal.com/?json=get_post&post_id=436"]];
NSDictionary *dicData = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&error];
NSDictionary *postData = [dicData objectForKey:@"post"];
NSString *title = [postData objectForKey:@"title"];
NSLog(@"%@", title);
}
Upvotes: 1