Reputation: 389
I´ve been looking at this the entire day, looking other solutions but nothing, I can´t solve my problem.
I want to get the JSON of https://alpha-api.app.net/stream/0/posts/stream/global, parse it so I can extract the username and in a future other attributes like post, avatar...
This is my viewDidLoad
where I set up the connection with the URL and then I change it to NSData Object.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL
URLWithString:@"https://alpha-api.app.net/stream/0/posts/stream/global"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil error:nil];
NSError *jsonParsingError = nil;
NSArray *timeline= [NSJSONSerialization JSONObjectWithData:response
options:0 error:&jsonParsingError];
NSDictionary *user;
for(int i=0; i<[timeline count];i++)
{
user = [timeline objectAtIndex:i];
NSLog(@"Statuses: %@", [user objectForKey:@"username"]);
}
My program starts running and then it stops. I know when it stops(user = [timeline objectiAtIndex:i])
but I have no idea why... Another question: Would [user objectForKey:@"username"]
be enough to extract the usernames?
Upvotes: 0
Views: 195
Reputation: 1907
The timeline data is contained in a dictionary which can be accessed through the key "data". I would do something along the lines of:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL
URLWithString:@"https://alpha-api.app.net/stream/0/posts/stream/global"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil error:nil];
NSError *jsonParsingError = nil;
NSDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
NSArray *timelineArray;
if (responseObject) {
timelineArray = [responseObject objectForKey:@"data"];
NSDictionary *user; // user data
for (NSDictionary *status in timelineArray) {
user = [status objectForKey:@"user"];
NSLog(@"Status: %@", [status objectForKey:@"text"]);
NSLog(@"Status by user: %@\n\n", [user objectForKey:@"username"]);
}
}
}
Upvotes: 0
Reputation: 852
Your parsing logic is incorrectly assuming that timeline will be an NSArray when the structure of the data you are retrieving indicates that it will actually be an NSDictionary.
Upvotes: 0
Reputation: 3369
You're getting an error because the following line returns an NSDictionary, not an NSArray.
NSArray* timeline= [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
So it should be,
NSDictionary* timeline= [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
and your logic should be adjusted accordingly.
Upvotes: 1
Reputation: 5417
It's likely the web service is returning a JSON dictionary as opposed to an array. This would cause an unrecognized selector exception to throw on [timeline objectAtIndex:i]
. Print out what the request returns in the debugger, and if it's a dictionary you'll need to find how to access the array you are expecting before iterating.
Upvotes: 0