开发者

parsing JSON using objective C?

开发者 https://www.devze.com 2022-12-10 00:21 出处:网络
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

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 returningRes开发者_如何学Pythonponse: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.


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消