IPhone/iOS JSON 解析教程

作为一个学习经验,我想制作一个 iPhone 应用程序,它调用 webserver/webservice,检索 JSON 响应,并使用该响应填充 UITableView的行(假设它首先将 JSON 转换为 NSArray)。

有人知道什么有用的信息吗?

137165 次浏览

You will love this framework.

And you will love this tool.

For learning about JSON you might like this resource.

And you'll probably love this tutorial.

try out with this fastest JSON framework JSONKit. it's faster than normal JSON framework.

As of iOS 5.0 Apple provides the NSJSONSerialization class "to convert JSON to Foundation objects and convert Foundation objects to JSON". No external frameworks to incorporate and according to benchmarks its performance is quite good, significantly better than SBJSON.

This is the tutorial I used to get to darrinm's answer. It's updated for ios5/6 and really easy. When I'm popular enough I'll delete this and add it as a comment to his answer.

http://www.raywenderlich.com/5492/working-with-json-in-ios-5

http://www.touch-code-magazine.com/tutorial-fetch-and-parse-json-in-ios6/

Here's a link to my tutorial, which walks you through :

  • creating a JSON WCF Web Service from scratch (and the problems you'll want to avoid)
  • adapting it to read/write SQL Server data
  • getting an iOS 6 app to use the JSON servies.
  • using the JSON web services with JavaScript

http://mikesknowledgebase.com/pages/Services/WebServices-Page1.htm

All source code is provided, free of charge. Enjoy.

SBJSON *parser = [[SBJSON alloc] init];


NSString *url_str=[NSString stringWithFormat:@"Example APi Here"];


url_str = [url_str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:url_str]];


NSData *response = [NSURLConnection sendSynchronousRequest:request  returningResponse:nil error:nil];


NSString *json_string = [[NSString alloc] initWithData:response1 encoding:NSUTF8StringEncoding]


NSDictionary *statuses = [parser2 objectWithString:json_string error:nil];


NSArray *news_array=[[statuses3 objectForKey:@"sold_list"] valueForKey:@"list"];


for(NSDictionary *news in news_array)
{


@try {
[title_arr addObject:[news valueForKey:@"gtitle"]];    //values Add to title array


}
@catch (NSException *exception) {


[title_arr addObject:[NSString stringWithFormat:@""]];
}