Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取JSON并尝试解析它时出错[iOS]_Ios_Objective C_Json_Parsing - Fatal编程技术网

获取JSON并尝试解析它时出错[iOS]

获取JSON并尝试解析它时出错[iOS],ios,objective-c,json,parsing,Ios,Objective C,Json,Parsing,我一整天都在考虑这个问题,寻找其他解决方案,但什么都没有,我无法解决我的问题 我想得到的JSON,解析它,这样我就可以提取用户名,并在未来的其他属性,如post,avatar。。。 这是我的viewDidLoad,我在这里设置了与URL的连接,然后将其更改为NSData对象 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a n

我一整天都在考虑这个问题,寻找其他解决方案,但什么都没有,我无法解决我的问题

我想得到的JSON,解析它,这样我就可以提取用户名,并在未来的其他属性,如post,avatar。。。 这是我的
viewDidLoad
,我在这里设置了与URL的连接,然后将其更改为NSData对象

- (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"]);
}
-(void)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
NSURLRequest*request=[NSURLRequest requestWithURL:[NSURL]
URLWithString:@“https://alpha-api.app.net/stream/0/posts/stream/global"]];
NSData*响应=[NSURLConnection sendSynchronousRequest:请求
returningResponse:nil错误:nil];
N错误*jsonParsingError=nil;
NSArray*timeline=[NSJSONSerialization JSONObjectWithData:响应
选项:0错误:&jsonParsingError];
NSDictionary*用户;

对于(int i=0;i,web服务很可能返回的是JSON字典而不是数组。这将导致在
[timeline objectAtIndex:i]上引发无法识别的选择器异常
。打印出请求在调试器中返回的内容,如果是字典,则需要在迭代之前找到如何访问所需的数组。

由于以下行返回NSDictionary,而不是NSArray,因此出现错误

NSArray* timeline= [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
所以应该是,

NSDictionary* timeline= [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];

并且您的逻辑应该相应地进行调整。

当您正在检索的数据的结构表明它实际上是一个NSDictionary时,您的解析逻辑错误地假设timeline将是一个NSArray。

timeline数据包含在可以通过键“data”访问的字典中.我会做一些大致如下的事情:

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

您了解了同步请求和异步请求之间的区别了吗?如果您使用的是同步请求,您必须确保它们不会在应用程序主线程中运行。您可能需要查看apple文档中的异步请求:请求只返回url:返回的是URL返回的是一个{.JSON,它包含一个字典{}或一个数组[]。您的包含一个字典。将结果分配给NSArray*不会使其成为数组。获取您正在使用的API的规范,或者您必须对其进行反向工程,这并不困难。我更改了您所说的内容。问题是,现在,当我尝试打印用户时,返回null:S NSDictionary*user;for(int i=0;数据结构本身比代码所允许的要复杂得多。您假设有一个“用户名”直接在时间线中,事实并非如此。它在JSON中隐藏得更深。我可能还会在生产就绪代码中添加一些额外的检查,以检查响应中的错误、缺少键等。您的答案是有效的。它仍然将所有状态打印为null,但这与从JSON中正确提取用户名有关,我想我会在白天找到答案的!谢谢你!@iostaicted,我添加了一些额外的打印输出(状态文本以及制作它的用户),希望它有帮助!太棒了。这就是我所需要的!我真的很感谢你的帮助!是否也可以提取头像并将其存储为UIImage?