Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 views
in Technique[技术] by (71.8m points)

parsing JSON using objective C?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...