如何解析JSON数据并在iphone应用程序中显示内容
我想知道,当我使用like时,数组中的对象如何将它们存储在objectlike中如何解析JSON数据并在iphone应用程序中显示内容,iphone,objective-c,json,Iphone,Objective C,Json,我想知道,当我使用like时,数组中的对象如何将它们存储在objectlike中 myArray.id= myArray.name= 所以它显示了价值 SBJsonParser *parser = [[SBJsonParser alloc] init]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.krsconnect.no/community/api.html?m
myArray.id=
myArray.name=
所以它显示了价值
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.krsconnect.no/community/api.html?method=categories&appid=620&mainonly=true"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSDictionary *object = [parser objectWithString:json_string error:nil];
NSArray *results = [parser objectWithString:json_string error:nil];
Matt Gallagher提供了关于如何解析JSON的精彩教程。希望这能让你朝着正确的方向开始 下面应该显示代码/JSON的结果:
// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSDictionary *object = [parser objectWithString:json_string error:nil];
// the JSON response is an Array
NSArray *results = [parser objectWithString:json_string error:nil];
NSDictionary *dictOne = [results objectAtIndex:0];
NSArray *activitiesArray = [dictOne objectForKey:@"activities"];
NSDictionary *dictTwo = [activitiesArray objectAtIndex:0];
NSDictionary *eventDict = [dictTwo objectForKey:@"event"];
NSLog(@"%@ - %@", [eventDict objectForKey:@"category"]);
Matt Gallagher提供了关于如何解析JSON的精彩教程。希望这能让你朝着正确的方向开始 下面应该显示代码/JSON的结果:
// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSDictionary *object = [parser objectWithString:json_string error:nil];
// the JSON response is an Array
NSArray *results = [parser objectWithString:json_string error:nil];
NSDictionary *dictOne = [results objectAtIndex:0];
NSArray *activitiesArray = [dictOne objectForKey:@"activities"];
NSDictionary *dictTwo = [activitiesArray objectAtIndex:0];
NSDictionary *eventDict = [dictTwo objectForKey:@"event"];
NSLog(@"%@ - %@", [eventDict objectForKey:@"category"]);
这是一条路要走
但是,此JSON无效。告诉提供API的人,确保他们的JSON返回是正确的 这是一条路要走
但是,此JSON无效。告诉提供API的人,确保他们的JSON返回是正确的 JSONKit使这变得非常简单,可能值得考虑。而且速度非常快。JSONKit使这变得非常简单,可能值得考虑。而且速度非常快。从json调用数据时,需要注意一些事项,但这非常简单。首先,您应该在m文件顶部添加以下两行:
#define kAsyncModus dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define kYourJsonURL [NSURL URLWithString: @"_THE_JSON_URL_"]
然后,在viewDidLoad方法中插入以下部分。“dispatch_async”必须与先前定义的宏(在顶部)一起使用,因此您可以创建一个线程。否则,如果服务器没有响应,应用程序可能会崩溃,但方法会一直等待响应
// get elements of json
dispatch_async(kAsyncModus, ^{
NSData *data = [NSData dataWithContentsOfURL: kYourJsonURL];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data
waitUntilDone:YES];
});
并创建一个新方法,从接收到的json中获取单个元素
- (void)fetchedData:(NSData *)responseData{
NSError *error;
NSDictionary *json =
[NSJSONSerialization JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSArray *results = [json objectForKey:@"_firstElementInJsonFile_"];
NSLog(@"Results: %@", results);
}
有关更多详细信息,请使用结果
NSDictionary *specificData = [results objectAtIndex:0];
应该这样做
我只是用最简单的方式解释了一下,如果你想让所有的事实都看一下下面。
但我不确定是否支持使用
#define kAsyncModus dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define kYourJsonURL [NSURL URLWithString: @"_THE_JSON_URL_"]
然后,在viewDidLoad方法中插入以下部分。“dispatch_async”必须与先前定义的宏(在顶部)一起使用,因此您可以创建一个线程。否则,如果服务器没有响应,应用程序可能会崩溃,但方法会一直等待响应
// get elements of json
dispatch_async(kAsyncModus, ^{
NSData *data = [NSData dataWithContentsOfURL: kYourJsonURL];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data
waitUntilDone:YES];
});
并创建一个新方法,从接收到的json中获取单个元素
- (void)fetchedData:(NSData *)responseData{
NSError *error;
NSDictionary *json =
[NSJSONSerialization JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSArray *results = [json objectForKey:@"_firstElementInJsonFile_"];
NSLog(@"Results: %@", results);
}
有关更多详细信息,请使用结果
NSDictionary *specificData = [results objectAtIndex:0];
应该这样做
我只是用最简单的方式解释了一下,如果你想让所有的事实都看一下下面。
但我不确定是否支持使用
activities
的NSArray和一个NSDictionary,它本身包含键事件
的NSDictionary,其中包含键年龄限制18的值。你的JSON解析代码需要反映JSON代码的结构。你能给我一个正确教程的链接吗?这只是描述fetcher这里有一些额外的链接:你看到我在哪里发布的链接了吗。您是否在代码中获得JSON响应,是否可以使用NSLog()记录它?接下来,您需要从JSON响应中获取活动的NSArray。您在发布的代码中返回的状态对象中的NSDictionary包含一个用于键activities
的NSArray和一个NSDictionary,它本身包含键事件
的NSDictionary,其中包含键年龄限制18的值。您的JSON解析代码需要反映JSON代码的结构。根据几个不同的JSON验证程序运行,这似乎不是有效的JSON。除此之外,对于一个API调用来说,这是一个相当大的数据返回-您确定需要一次检索所有数据吗?是的,检索所有数据,或者是否有任何工具可以将其转换为xml格式,然后进行检索?我认为这在性能方面并不值得。使用上面链接的SBJSon框架,JSON非常快速且易于解析。这里的问题是负责提供API的一方没有生成有效的JSON。编辑了我的答案。根据几个不同的JSON验证程序运行,这似乎不是有效的JSON。除此之外,对于一个API调用来说,这是一个相当大的数据返回-您确定需要一次检索所有数据吗?是的,检索所有数据,或者是否有任何工具可以将其转换为xml格式,然后进行检索?我认为这在性能方面并不值得。使用上面链接的SBJSon框架,JSON非常快速且易于解析。这里的问题是负责提供API的一方没有生成有效的JSON。编辑我的答案。JSONKit是否适用于使用iOS 4.2-5.0的设备?似乎是因为他们在JSONKit github页面自述中比较了苹果提供的NSJSONSerialization和JSONKit:JSONKit是否适用于使用iOS 4.2-5.0的设备?似乎是因为他们在比较苹果提供的NSJSONSerialization和JSONKit上的JSONKitgithub页面自述: