简体   繁体   中英

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 :

    {    
        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". But the program is crashing. What am I doing wrong?

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

Given JSON response is invalidate. Validate your JSON response here .

You likely don't have the complete data yet in -connection:didReceiveResponse: . Create an instance variable or property of the type NSMutableData and initialize the data ivar or property in
-connection:didReceiveResponse: if you get a valid statusCode (between 200-299 should be ok). Use appendData: on the data object in the -connection:didReceiveData: delegate method. Finally in -connectionDidFinishLoading: the data is complete and can be parsed into JSON.

Alternatively you could just use the AFNetworking library. The library got some convenience methods for dealing with XML, JSON, images, etc...

Read the following page to get an introduction into the capabilities of 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. The URL Request objects are a custom subclass of NSURLConnection for some block "callbacks":

#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];
}

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