0

I have spent 1 week studying objective C. Now I am quite confused at the dealing with data part. My friend gave me a link http://nrj.playsoft.fr/v3/getQuiz.php?udid=23423455&app=2 and ask me write a class to parse this JSON. I had no clue what parsing JSON means. but I have gone online and looked up. I could understand a basics of it and then I impletemented a punch of code to parse this JSON. Which is:

-

(void)parseURL
{
    //create new SBJSON object 
    SBJSON *parser = [[SBJSON alloc] init];
    NSError *error = nil;
    //perform request from URL 
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://nrj.playsoft.fr/v3/getQuiz.php?udid=23423455&app=2"]];
    // Perform request and get JSON back as a NSData object
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];

    // Get JSON as a NSString from NSData response
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

    // parse the JSON response into an object

    NSDictionary *results = [parser objectWithString:json_string error:&error];
    // array just for the "answer" results
    NSArray *quizes = [results objectForKey:@"quiz"];

    NSDictionary *firstQuiz = [quizes objectAtIndex:0];
    // finally, the name key
    NSString *extract = [firstQuiz objectForKey:@"extract"];
    NSLog(@"this is: %@", [extract valueForKey:@"extract"]); 

}

This is at the implementation file, but in the header file I could not declare any variables, it will print out some errors. I tried to run this, there is no errors, but I am not sure this code is correct or not. And my friend asked me to write a class into an existing project. I don't know what needs to be modified and what not. I am so blur right now. Could anyone pro in this give me a hand. ? My sincere thanks.


Thanks for reply. I have downloading and added the JSON framework ealier too. I am just not sure where to begin and where to end, meaning the step I should do when I add JSON framework into it. I could understand the syntax but I am not sure about the steps I should do. I am a newbie in this.

0

3 Answers 3

4

If you support iOS 5.0+, you should use built-in NSJSONSerialization. It is faster than TouchJSON.

Sign up to request clarification or add additional context in comments.

2 Comments

Congratulations, you just answered a question that is almost 4 years old and already has an answer. Whilst you are correct, back in October 2009 NSJSONSerialization wasn't invented yet.
@Fogmeister Welcome to 2013! The accepted answer is outdated and this an excellent reason for posting a new updated one. Anybody coming here looking for a JSON parsers will now know that Apple has a built-in one.
1

You could just use TouchJSON: http://code.google.com/p/touchcode/wiki/TouchJSON

Or you could use this one: http://code.google.com/p/json-framework/

I'm sure there are others. I use TouchJSON... it's fast and has a good API.

Comments

-1

I recommend working through Ray Wenderlich's MapKit tutorial, especially if you are a newbie. It covers several common iOS development issues, including parsing JSON data.

http://www.raywenderlich.com/2847/introduction-to-mapkit-on-ios-tutorial

"The Implementation" section is where his JSON feed is retrieved and then in "Plotting the Data" he uses the SBJson library to parse it.

1 Comment

It'll be easier for you to understand how to add something like this to an existing project if you work through a tutorial like this that starts from scratch.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.