-1

NSJSONSerialization returns null when i try to implement a particular api from the server, but other api's from the server works fine , when i tried the api in web it returns data of json type , and i have checked it in json viewer also , and it worked fine , NSData also returns some data, can Anyone help me get through it ?

NSString *urlString = @"url";        
NSURL *url = [NSURL URLWithString:urlString];
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfURL:url];
_dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

if(error != nil){

    NSLog(@"JSON Error: %@", error);
    return;
}

I have tried the above written code.

---Edited----- Since it was a issue from server side as server was sending an Invalid Character along with response, In json validator it was passing but not in a actual Program , Hence closing the question.

vinay
  • 351
  • 1
  • 5
  • 16
  • thanks guys for your useful answers. actually the problem was with the server , they have added an invalid character in the json. – vinay Jul 13 '13 at 16:45
  • I hav checked it using `[NSJSONSerialization isValidJson]` which tells weather the json returned is a valid one or not. – vinay Jul 13 '13 at 16:47
  • What error is being returned?? – Hot Licks Aug 17 '13 at 13:01
  • I'm voting to close this question as off-topic because the problem was not in the code but in the server data coming as response. – vinay Nov 15 '19 at 05:34

2 Answers2

2

The Problem is of Escape Sequences in your URL and you are not escaping them. You have to escape those sequences properly.

NSString *urlString = @"url";        
NSString *encodedString = [urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:encodedString];
  1. Take a Look at Documentation.
  2. Check also the Escape Sequences.

Try this :

NSString *urlString = @"url";        
NSURL *myUrl = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Community
  • 1
  • 1
Bhavin
  • 27,155
  • 11
  • 55
  • 94
  • @vinay.bn : If your URL is working in browser and not working in xcode then it is the problem of escape sequences only. Check the Documentation also. – Bhavin Jul 11 '13 at 13:26
  • @vinay.bn: Take a look at my Update Section. – Bhavin Jul 11 '13 at 13:32
  • :the url doesn't contain any escape character. – vinay Jul 11 '13 at 13:32
  • @vinay.bn: Just use that code in Update and let me know if it's working or not. – Bhavin Jul 11 '13 at 13:34
  • `NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];` Use this line to fetch the data and then `NSLog` it. – Bhavin Jul 11 '13 at 13:43
  • @vinay.bn: What was the result of that `NSLog` ? – Bhavin Jul 11 '13 at 13:59
  • I encoded the data and it returns the desired result, but not able to get form the NSJsonserialization class... – vinay Jul 11 '13 at 14:17
  • @vinay.bn: What's the result that you are getting ? Is it an Error Message ? – Bhavin Jul 11 '13 at 14:21
  • when i try to read the nsdata through encoding it returns result as i xpected, but not able to get it form nsjsonserialization – vinay Jul 11 '13 at 14:47
  • :is there a way to parse json data without using nsserialization, bcoz i am getting data correct but not able to parse it using nsjsonsserialization – vinay Jul 11 '13 at 14:57
  • 1
    @vinay.bn: That means your JSON Data is not in proper format. – Bhavin Jul 12 '13 at 06:40
  • I am getting NSdata and if I encode the data i will get the nsstring of values, to check weather its problem in server return format I have tried in `http://jsonviewer.stack.hu` website and it returns the value which i need inthe format of json,probably it might be the problem in NSJSONSerialization. – vinay Jul 12 '13 at 12:25
  • `NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];` Try this way. I think the problem is in initialization of your Dictionary. – Bhavin Jul 13 '13 at 11:27
  • 1
    @vinary.bn Would you please mark the answer as correct? It is only fair that you give Vin the reps since he helped a lot and you solved you issue due to his help, so I think it would be more than appropriate after more than a month... – HAS Aug 17 '13 at 15:47
1

Make a NSURLConnection and request the JSON with NSURLRequest because I don't think the 'json' is an actual file that has content so you could use dataWithContentsOfURL:

soryngod
  • 1,827
  • 1
  • 14
  • 13