我正在编写一个使用Instagram API的程序,但NSMutableData and NSDictionary.遇到了一些问题

由于必须多次调用该API,因此我决定创建一个NSMutableData object,将较小的NSData对象附加到该对象,然后将整个对象转换为NSDictionary。

但是,当我再次将NSMutableData appendData NSData,转换为NSMutableData时再次调用NSDictionary后,字典返回null。

这是我的一些代码。

NSMutableData *userData = [[NSMutableData alloc]init]
NSData *feed = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString   stringWithFormat:@"https://api.instagram.com/v1/users/17636367/media/recent?access_token=%@",accessToken]]];
[userData appendData:feed];
NSData *moarData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.instagram.com/v1/users/17636367/media/recent?access_token=%@&max_id=%@",accessToken, maxID]]];
[userData appendData:moarData];
NSDictionary *dictTwo = [NSJSONSerialization JSONObjectWithData:userData options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",dictTwo);

dictTwo返回null。但是,当我只对appendData进行一次调用时,字典不是空的。

谢谢。

最佳答案

当您附加两个单独的JSON响应时,它们将不会形成JSON。

您可以通过以下链接测试最终结果以查看是否为JSON:

JSON validator

您需要分别解析每个查询响应,然后对该数据进行操作:(例如NSMutableDictionary,NSMutableArray等)。

10-08 07:08