简体   繁体   English

无法解析来自NSURLConnection响应的JSON数据

[英]unable to parse JSON data from a NSURLConnection response

I am getting a server response of the form: 我收到以下形式的服务器响应:

results are:{
    AverageMark = 40;
    "Grade A" = 10;
    "Grade B" = 20;
    "Grade C" = 30;
    "Grade D" = 20;
    MaxMark = 99;
    MinMark = 44;
    ProfileGrade = "";
    ProfileMark = 1;
}

However I am unable to save the response data into an Array. 但是我无法将响应数据保存到数组中。 This is my code inside didReceiveResponse : 这是我在didReceiveResponse中的代码:

    {    
        NSString *jsonString = [[NSString alloc] initWithString:responseData];
        NSArray *jsonResults = [jsonString JSONValue];
        NSLog(@"results are:%@",jsonResults); //this log is shown above
        for (int i=0; i<[jsonResults count]; i++)
        {
            NSDictionary *AllData=(NSDictionary *)[jsonResults objectAtIndex:i]; //Program is crashing here--//
            NSMutableArray  *DataArray=[[NSMutableArray alloc]init];
            NSString *avgMarkString;
            avgMarkString=(NSString *)[AllData objectForKey:@"MaxMark"];
            [DataArray addObject:avgMarkString];
        }
    }

I want to save the response data into the array called "DataArray". 我想将响应数据保存到名为“ DataArray”的数组中。 But the program is crashing. 但是程序崩溃了。 What am I doing wrong? 我究竟做错了什么?

不是json,请尝试看看此http://json.org/example.html

Given JSON response is invalidate. 给定的JSON响应无效。 Validate your JSON response here . 在此处验证您的JSON响应。

You likely don't have the complete data yet in -connection:didReceiveResponse: . -connection:didReceiveResponse:可能还没有完整的数据。 Create an instance variable or property of the type NSMutableData and initialize the data ivar or property in 创建类型为NSMutableData的实例变量或属性,并在其中初始化数据ivar或属性
-connection:didReceiveResponse: if you get a valid statusCode (between 200-299 should be ok). -connection:didReceiveResponse:如果获得有效的statusCode(200-299之间应该可以)。 Use appendData: on the data object in the -connection:didReceiveData: delegate method. -connection:didReceiveData:委托方法的数据对象上使用appendData: Finally in -connectionDidFinishLoading: the data is complete and can be parsed into JSON. 最后在-connectionDidFinishLoading:数据已完成,可以解析为JSON。

Alternatively you could just use the AFNetworking library. 或者,您可以只使用AFNetworking库。 The library got some convenience methods for dealing with XML, JSON, images, etc... 该库提供了一些方便的方法来处理XML,JSON,图像等。

Read the following page to get an introduction into the capabilities of AFNetworking: http://engineering.gowalla.com/2011/10/24/afnetworking/ 阅读以下页面以介绍AFNetworking的功能: http ://engineering.gowalla.com/2011/10/24/afnetworking/


Some example code from one of my own projects for downloading using a queue using NSURLConnectionDelegate methods. 我自己的项目之一提供的一些示例代码,可使用NSURLConnectionDelegate方法使用队列下载。 The URL Request objects are a custom subclass of NSURLConnection for some block "callbacks": URL请求对象是NSURLConnection的自定义子类,用于某些块“回调”:

#pragma mark - URL connection delegate

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

    NSRange range = NSMakeRange(200, 99);
    if (NSLocationInRange(httpResponse.statusCode, range));
    {
        self.data = [[NSMutableData alloc] init];
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_data appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // inform caller that download is complete, provide data ...

    if (_request.completionHandler)
    {
        _request.completionHandler(_data, nil);
    }

    [self removeRequest:_request];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    DLog(@"%@", error);

    // inform caller that download failed, provide error ...

    if (_request.completionHandler)
    {
        _request.completionHandler(nil, error);
    }

    [self removeRequest:_request];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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