如何提取 JSON 数组?
How to extract JSON array?
如何提取 JSON
数据 dictionary
。我尝试了很多次,但无法提取特定的价值。我只得到第一个 "business_category_name" 但 "business_details" 低于值 "name" 无法获取。怎么可能请帮助。
谢谢
我的JSON数据
{
"status": [
{
"business_category_name": "Banks\/Credit Unions\/Landers",
"business_details": [
{
"img_url": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/member-logo\/1464667619-pnc.jpg",
"name": "PNC Bank",
"des": "PNC offers a wide range of services for all our customers, from individuals and small businesses, to corporations and government entities",
"address": "",
"email": "andrea.kendall@pnc.com",
"phone": "260-422-5922",
"member_since": "2016-05-31",
"img_company": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/member-logo\/1464667619-pnc.jpg",
"website": "",
"city": "Fort Wayne",
"state": "IN",
"zip": "46808"
}
]
},
{
"business_category_name": "Cleaning Services",
"business_details": [
{
"img_url": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/uploads\/logo250.png",
"name": "tsst company",
"des": "rudurgg ",
"address": "2005 s calhoun ",
"email": "",
"phone": "2602496687",
"member_since": "2016-05-31",
"img_company": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/uploads\/logo250.png",
"website": "",
"city": "fort wayne",
"state": "in",
"zip": "46825"
}
]
}
]
}
我的代码
- (void)viewDidLoad {
[super viewDidLoad];
NSURLRequest *req=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"my url"]];
response =[[NSMutableData alloc]init];
[NSURLConnection connectionWithRequest:req delegate:self];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error;
NSLog(@"Error in receiving data %@",error);
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSLog(@"response data %@",json);
NSArray *items = [json objectForKey:@"status"];
pass = [items valueForKey:@"business_category_name"];
NSLog(@"get category %@",pass);
}
如果你只想从数组中获取一个字典试试这个
NSArray *statuses = [json objectForKey:@"status"];
for(NSDictionary *status in statuses){
NSString *bussiness_category=[status valueForKey:@"business_category_name"];
NSString *name=[[[status objectForKey:@"business_details"] firstObject] valueForKey:@"name"];
}
如果你想全部获取,试试这个。
创建一个可变数组并将所有名称添加到其中并加载到表视图中,如下所示
NSMutableArray *names = [NSMutableArray new];
NSArray *statuses = [json objectForKey:@"status"];
for(NSDictionary *status in statuses){
NSString *bussiness_category=[status valueForKey:@"business_category_name"];
for(NSDictionary *details in [status objectForKey:@"business_details"]){
NSString *name=[details valueForKey:@"name"];
[names addObject:name];
}
}
希望对您有所帮助。
你可以这样做,
NSArray *items = [json objectForKey:@"status"];
NSDictionary *firstObjOfItems = [items objectAtIndex:0];
NSString *business_category_name = [firstObjOfItems valueForKey:@"business_category_name"];
NSArray *business_details_Array = [firstObjOfItems objectForKey:@"business_details"];
NSDictionary *first_Object_of_business_details = [business_details_Array objectAtIndex:0]
;
NSString *name = [first_Object_of_business_details valueForKey:@"name"];
NSLog(@"name : %@",name);
您可以同样获取其他参数。您应该使用 for 循环遍历每个对象。这只是如何到达 json 数据中的结束元素的示例。
希望这会有所帮助:)
这里dataFromApi是字符串JSON格式;
NSData *objectData = [dataFromApi dataUsingEncoding:NSUTF8StringEncoding];
id collection = [NSJSONSerialization JSONObjectWithData:objectData options:0 error:&error];
收到集合后,您可以使用键值对查找数据。
ex = string add = collection[@"name"];
如何提取 JSON
数据 dictionary
。我尝试了很多次,但无法提取特定的价值。我只得到第一个 "business_category_name" 但 "business_details" 低于值 "name" 无法获取。怎么可能请帮助。
谢谢
我的JSON数据
{
"status": [
{
"business_category_name": "Banks\/Credit Unions\/Landers",
"business_details": [
{
"img_url": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/member-logo\/1464667619-pnc.jpg",
"name": "PNC Bank",
"des": "PNC offers a wide range of services for all our customers, from individuals and small businesses, to corporations and government entities",
"address": "",
"email": "andrea.kendall@pnc.com",
"phone": "260-422-5922",
"member_since": "2016-05-31",
"img_company": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/member-logo\/1464667619-pnc.jpg",
"website": "",
"city": "Fort Wayne",
"state": "IN",
"zip": "46808"
}
]
},
{
"business_category_name": "Cleaning Services",
"business_details": [
{
"img_url": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/uploads\/logo250.png",
"name": "tsst company",
"des": "rudurgg ",
"address": "2005 s calhoun ",
"email": "",
"phone": "2602496687",
"member_since": "2016-05-31",
"img_company": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/uploads\/logo250.png",
"website": "",
"city": "fort wayne",
"state": "in",
"zip": "46825"
}
]
}
]
}
我的代码
- (void)viewDidLoad {
[super viewDidLoad];
NSURLRequest *req=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"my url"]];
response =[[NSMutableData alloc]init];
[NSURLConnection connectionWithRequest:req delegate:self];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error;
NSLog(@"Error in receiving data %@",error);
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSLog(@"response data %@",json);
NSArray *items = [json objectForKey:@"status"];
pass = [items valueForKey:@"business_category_name"];
NSLog(@"get category %@",pass);
}
如果你只想从数组中获取一个字典试试这个
NSArray *statuses = [json objectForKey:@"status"];
for(NSDictionary *status in statuses){
NSString *bussiness_category=[status valueForKey:@"business_category_name"];
NSString *name=[[[status objectForKey:@"business_details"] firstObject] valueForKey:@"name"];
}
如果你想全部获取,试试这个。
创建一个可变数组并将所有名称添加到其中并加载到表视图中,如下所示
NSMutableArray *names = [NSMutableArray new];
NSArray *statuses = [json objectForKey:@"status"];
for(NSDictionary *status in statuses){
NSString *bussiness_category=[status valueForKey:@"business_category_name"];
for(NSDictionary *details in [status objectForKey:@"business_details"]){
NSString *name=[details valueForKey:@"name"];
[names addObject:name];
}
}
希望对您有所帮助。
你可以这样做,
NSArray *items = [json objectForKey:@"status"];
NSDictionary *firstObjOfItems = [items objectAtIndex:0];
NSString *business_category_name = [firstObjOfItems valueForKey:@"business_category_name"];
NSArray *business_details_Array = [firstObjOfItems objectForKey:@"business_details"];
NSDictionary *first_Object_of_business_details = [business_details_Array objectAtIndex:0]
;
NSString *name = [first_Object_of_business_details valueForKey:@"name"];
NSLog(@"name : %@",name);
您可以同样获取其他参数。您应该使用 for 循环遍历每个对象。这只是如何到达 json 数据中的结束元素的示例。
希望这会有所帮助:)
这里dataFromApi是字符串JSON格式;
NSData *objectData = [dataFromApi dataUsingEncoding:NSUTF8StringEncoding];
id collection = [NSJSONSerialization JSONObjectWithData:objectData options:0 error:&error];
收到集合后,您可以使用键值对查找数据。
ex = string add = collection[@"name"];