简体   繁体   中英

Error getting JSON and trying to parse it [iOS]

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?

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.

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.

当您正在检索的数据结构表明它实际上将是NSDictionary时,您的解析逻辑错误地认为时间轴将是NSArray。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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