Reputation: 13787
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.hrjournalmyanmar.com/test.cfm"]];
__block NSDictionary *json;
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
json = [NSJSONSerialization JSONObjectWithData:data
options:0
error:nil];
NSLog(@"Async JSON: %@", json);
I cannot convince why I got (null) return message from above even json format and coding are correct. Above coding is correct when json data (field length) is small but field length is long and large, (null) error occurred.
Upvotes: 1
Views: 466
Reputation: 107121
Your JSON is not valid. Please check it in JSONLint or JSON Editor
Issue is with the thumb
key, the opening "
is missing for that key.
In your JSON it's like:
thumb":"http: //mmjobs.mmdroid.biz/articles/articles234."
It should be like:
"thumb":"http: //mmjobs.mmdroid.biz/articles/articles234."
EDIT:
Based on your comments.
Still your JSON is not valid it contains a lot of new line characters and it makes it invalid. I solved your issue using the following method.
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.hrjournalmyanmar.com/test.cfm"]];
__block NSDictionary *json;
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
NSError *error = nil;
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\r" withString:@""];
json = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:&error];
if (!error)
{
NSLog(@"Async JSON: %@", json);
}
else
{
NSLog(@"Error: %@", error);
}
}];
Note:
I won't recommend this approach, it's better to change the JSON at server side itself. Use the error argument, instead of passing nil to it. The error object will give you the exact error message.
Upvotes: 2
Reputation: 111
The issue is that you aren't creating a valid JSON format in your backend. If you try my snippet, works perfectly, but instead of creating my data in JSON is a NSString.
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.hrjournalmyanmar.com/test.cfm"]];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSString* dataReceived = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Async JSON: %@", dataReceived);
}];
Upvotes: 0