60

I have used this method,

NSDictionary *jsonObject=[NSJSONSerialization 
       JSONObjectWithData:jsonData 
                  options:NSJSONReadingMutableLeaves 
                    error:nil];
NSLog(@"jsonObject is %@",jsonObject);

It's printing "jsonObject is null".
Is there any problem with "error:nil".
I am not using any url or connection methods.
I have a json file and I want to display it in a table.

serge-k
  • 3,394
  • 2
  • 24
  • 55
Rajesh
  • 937
  • 1
  • 8
  • 14
  • Did you check that jsonData is not nil? You may want to pass an NSError object to the call if you want to get some error description. – onnoweb Sep 26 '12 at 13:43
  • 1
    You can find answer here [How to use NSJSONSerialization][1] [1]: http://stackoverflow.com/questions/8356842/how-to-use-nsjsonserialization – Ivan Dyachenko Sep 26 '12 at 13:45
  • @ Ivan Dyachenko: i too did d same thing..... "NSError *myerror=nil; then "error:&myerror;" even then i got null...... – Rajesh Sep 26 '12 at 13:52
  • This should be called "JSON serialized data to NSDictionary", NSData to NSDictionary and vice versa conversion answer can be found here... http://stackoverflow.com/q/5513075/4018041 – serge-k Mar 01 '16 at 19:06

5 Answers5

155

Please Try the following Code.

NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData
                                                     options:kNilOptions 
                                                       error:&error];

NSArray* latestLoans = [json objectForKey:@"loans"];

NSLog(@"loans: %@", latestLoans);
estemendoza
  • 3,023
  • 5
  • 31
  • 51
amit soni
  • 2,183
  • 1
  • 14
  • 17
  • 4
    then enter your url in browser and then you will get dict or array then copy all data and paste it in this link http://jsonlint.com/ if your json is valid then it will show or if there is any error then will show null. – amit soni Sep 27 '12 at 07:24
9

Just in case anyone is here to see swift code:

NSData to NSDictionary

let dictionary:NSDictionary = NSKeyedUnarchiver.unarchiveObjectWithData(jsonData)! as NSDictionary

NSData to NSString

let resstr = NSString(data: res, encoding: NSUTF8StringEncoding)
iAhmed
  • 6,556
  • 2
  • 25
  • 31
  • 1
    for swift 4: let dictionary:NSDictionary = NSKeyedUnarchiver.unarchiveObject(with: data)! as! NSDictionary – zaheer Aug 16 '18 at 09:35
6

Check your json input, could it be that your root element is neither a dictionary nor an array? The documentation says you have to specify NSJSONReadingAllowFragments as the option in this case.

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
    NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];

    NSData *jsonData = [@"{ \"key1\": \"value1\" }" dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *jsonObject=[NSJSONSerialization 
           JSONObjectWithData:jsonData 
                      options:NSJSONReadingMutableLeaves 
                        error:nil];
    NSLog(@"jsonObject is %@",jsonObject);

    [p release];
}

Output:

2012-09-26 16:06:51.610 Untitled[19164:707] jsonObject is {
    key1 = value1;
}
0
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

This is just a more succinct version of the top post.

Luis B
  • 1,684
  • 3
  • 15
  • 25
-1

let convertedJsonIntoDict = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary

Softlabsindia
  • 791
  • 6
  • 11