Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to get my head around the NSJSONSerialization Class Reference. In the lack of a code-example at the developer.apple.com website, I´m lost. There are millions of examples all around the web with other json libaries, but I haven´t been able to get any of them to work with the latest build of xcode. (I´m running: Version 4.3.1 (4E1019) and testing on iPhone 5.0.1)

I want to fetch the data from the json file into my iphone using a button.

Lets say I get my data from the URL: http://companyurl/jsonfile.json (standard JSON-format)

The jsonfile.json looks somthing like this…;

{
  "companylist":   
[
      {
        "company":"Companyname 1",
        "telephone":"1234567890",
        "url":"http:\/\/www.companyname1.com\/",
        "category":"category 1",
        "position":"1",
      },
      {
        "company":"Companyname 2",
        "telephone":"2345678901",
        "url":"http:\/\/www.companyname2.com\/",
        "category":"category 2",
        "position":"2",
      },
      {
        "company":"Companyname 3",
        "telephone":"3456789012",
        "url":"http:\/\/www.companyname3.com\/",
        "category":"category 3",
        "position":"3",
      }
]
}

What do I write in my .h, and my .m file?

Thanks for any help! :)

share|improve this question
It helps to search before asking: how to use NSJSONSerialization, retrieving data from url – rickster Mar 17 '12 at 21:19
add comment (requires an account with 50 reputation)

4 Answers

NSString *str=@"http://your_web_server/your_file....";
NSURL *url=[NSURL URLWithString:str];
NSData *data=[NSData dataWithContentsOfURL:url];
NSError *error=nil;
id response=[NSJSONSerialization JSONObjectWithData:data options:
                                NSJSONReadingMutableContainers error:&error]; 

NSLog(@"Your JSON Object: %@ Or Error is: %@", response, error);

NOTE: This code works on Xcode 4.2 with iOS 5.01 on simulator and 5.1 on iPad device at the moment

share|improve this answer
response is id, not id* – hop Dec 16 '12 at 15:15
add comment (requires an account with 50 reputation)
up vote 5 down vote accepted

Thanks guys. I figured it out. (...and here is what I did:)

In my .m file I added this code:

    - (IBAction)getDataFromJson:(id)sender {
        NSURL *url = [NSURL URLWithString:@"http://yourwebsite.com/jsonfile.json"];

        NSData *jsonData = [NSData dataWithContentsOfURL:url];


        if(jsonData != nil)
        {
            NSError *error = nil;
            id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
            if (error == nil)
                NSLog(@"%@", result);
}
}

In my .h file, I added this code:

@interface ViewController : UIViewController
- (IBAction)getDataFromJson:(id)sender;
share|improve this answer
add comment (requires an account with 50 reputation)

There is a code example in the form of the Tweeting sample app.

share|improve this answer
add comment (requires an account with 50 reputation)

There's a great tutorial here. http://www.raywenderlich.com/5492/working-with-json-in-ios-5

share|improve this answer
(I didnt get this one to work...-but I might have missed something.) – CustomCase Mar 30 '12 at 8:41
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.