[英]How to output JSon data in Objective-C
我目前正在使用一个iPhone应用程序,该应用程序从以下来源获取数据:
我试图弄清楚如何在一个文本字段中将其解析为人类可读的格式。
到目前为止,我的代码是:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlString = [NSString stringWithFormat:@"http://dev.threesixtyapp.com/api/events.php?action=available&id=1"];
NSURL *url =[NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"%@",json);
}
http://stig.github.com/json-framework/-SBJson是用于编码/解码JSON的出色框架。 我建议您将其签出...它将为您解析为NSDictionary,您只需将文本字段的文本设置为等于所需的NSDictionary中的值即可。 使用此框架非常简单。 当您将Json传递给SBJson函数时,它应该只是一个字符串
首先,您必须了解json的数据结构。
您可以使用JSON Viewer来查看json的数据结构。
如我所见,您正在获取由event_title
, date_from
和date_to
组成的对象数组。
NSError *error = nil;
NSArray *jsonArry = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"%@",jsonArry);
for (NSDictionary *dict in jsonArry) {
NSString * title = [dict objectForKey:@"event_title"];
NSString * dateTo = [dict objectForKey:@"date_to"];
NSString * dateFrom = [dict objectForKey:@"date_from"];
NSLog(@"title=%@,dateTo=%@,dateFrom=%@",title,dateTo,dateFrom);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.