parsingJSON – iPhone

我是json的新手,我需要你的帮助。

我收到了像这样的JSONstring:

{"network": { "network_id":111, "name":"test name", "city":"test city", "country":"test country", "description":"test desc" } } 

我如何处理这个string和拆分键/值,以便在我的视图中使用它们?

 - (void) connectionDidFinishLoading:(NSURLConnection *)connection { [connection release]; NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding]; self.responseData = nil; //*********** How I can parse responseString *********// [networkIDLabel setText:@"ADD THE VALUE"]; [nameLabel setText:@"ADD THE VALUE"]; [responseString release]; [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; } 

谢谢

在Objective-C中,json可以被视为Dictionary

 -(void)getData:(NSData*)response{ // You have to include the SBJSON or else you can also use the NSJSONSerialization //NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&erro]; SBJSON *parse = [[SBJSON alloc]init]; NSString *jsonString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; NSDictionary *jsonData = [parse objectWithString:jsonString error:&erro]; NSDictionary *insideData = [jsonData objectForKey:@"network"]; if(![insideData isKindOfClass:[NSNull class]]) { NSString *data1 = [insideData objectForKey:@"network_Id"]; NSString *data2 = [insideData objectForKey:@"name"]; } } 

在iOS 5及更高版本中,您可以使用NSJSONSerialization直接parsing响应数据:

 [NSJSONSerialization JSONObjectWithData:self.responseData …]; 

如果你想支持iOS的早期版本,你可以使用JSONKit 。