使用目标 C 解析 JSON?
我花了 1 周时间学习目标 C.现在我在处理数据部分很困惑.我朋友给了我一个链接http://nrj.playsoft.fr/v3/getQuiz.php?udid=23423455&app=2并让我写一个类来解析这个 JSON.我不知道解析 JSON 是什么意思.但我上网查了一下.我可以理解它的基础知识,然后我实现了一些代码来解析这个 JSON.这是:
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.
谢谢回复.我也下载并添加了 JSON 框架.我只是不确定从哪里开始和在哪里结束,这意味着我在向其中添加 JSON 框架时应该执行的步骤.我能理解语法,但我不确定我应该做的步骤.我是这方面的新手.
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.
你可以只使用 TouchJSON:http://code.google.com/p/touchcode/wiki/TouchJSON
You could just use TouchJSON: http://code.google.com/p/touchcode/wiki/TouchJSON
或者你可以使用这个:http://code.google.com/p/json-framework/
我确定还有其他人.我使用 TouchJSON……它速度很快,而且有一个很好的 API.
I'm sure there are others. I use TouchJSON... it's fast and has a good API.