简体   繁体   English

使用目标C解析JSON?

[英]parsing JSON using objective C?

I have spent 1 week studying objective C. Now I am quite confused at the dealing with data part. 我花了1周的时间研究目标C。现在,我对处理数据部分感到很困惑。 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. 我的朋友给了我一个链接http://nrj.playsoft.fr/v3/getQuiz.php?udid=23423455&app=2,并请我编写一个类来解析此JSON。 I had no clue what parsing JSON means. 我不知道解析JSON意味着什么。 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. 我了解了它的基础知识,然后添加了一堆代码来解析此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. 我已经下载并添加了JSON框架。 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. 我只是不确定从哪里开始和从哪里结束,这意味着当我向其中添加JSON框架时应该执行的步骤。 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 . 如果您支持iOS 5.0+,则应使用内置的NSJSONSerialization It is faster than TouchJSON . 比TouchJSON快

You could just use TouchJSON: http://code.google.com/p/touchcode/wiki/TouchJSON 您可以只使用TouchJSON: http : //code.google.com/p/touchcode/wiki/TouchJSON

Or you could use this one: http://code.google.com/p/json-framework/ 或者,您可以使用以下网址http : //code.google.com/p/json-framework/

I'm sure there are others. 我敢肯定还有其他人。 I use TouchJSON... it's fast and has a good API. 我使用的是TouchJSON ...它速度很快,并且具有良好的API。

I recommend working through Ray Wenderlich's MapKit tutorial, especially if you are a newbie. 我建议您阅读Ray Wenderlich的MapKit教程,特别是如果您是新手。 It covers several common iOS development issues, including parsing JSON data. 它涵盖了几个常见的iOS开发问题,包括解析JSON数据。

http://www.raywenderlich.com/2847/introduction-to-mapkit-on-ios-tutorial 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. 在“实现”部分中,检索其JSON提要,然后在“绘制数据”中,使用SBJson库对其进行解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM