我正在使用 URL 访问 JSON 文件
我从以下代码中得到这个
NSData* data=[jsonString dataUsingEncoding: [NSString defaultCStringEncoding] ];
NSError *error; //where jsonString is string of URL
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (jsonDictionary==nil) {
NSLog(@"Error::Loading");
}else
{
NSArray *array = [jsonDictionary objectForKey:@"categories"];
for (int i=0; i<[array count]; i++) {
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
[dic setValue:[[array objectAtIndex:i] valueForKey:@"cat_id"] forKey:@"cat_id"];
[dic setValue:[[array objectAtIndex:i] valueForKey:@"cat_name"] forKey:@"cat_name"];
[dic setValue:[[array objectAtIndex:i] valueForKey:@"cat_image"] forKey:@"cat_image"];
[dic setValue:[[array objectAtIndex:i] valueForKey:@"cat_list"] forKey:@"cat_list"];
[newsArray addObject:dic];
[categoryArry addObject:[dic valueForKey:@"cat_name"]];
[categoryId addObject:[dic valueForKey:@"cat_id"]];
[categoryImage addObject:[dic valueForKey:@"cat_image"]];
[categoryList addObject:[dic valueForKey:@"cat_list"]];
[dic release];
}
}
并得到这样的json结构
{
"categories": [
{
"cat_id": "15",
"cat_name": "Shop",
"cat_image": null,
"cat_list": [
{
"sub_cat_name": "New Arrivals",
"Cat_Data": [
{
"prod_name": "Generals Coat Biege",
"prod_price": "215.0000",
"prod_image": "honey_beau_lookbook_winter13_febmarch-24.jpg"
}
]
}
]
}
]
}
categoryArry 仅包含值“Shop”
,但我想访问 categoryArry 中的“sub_cat_name”并跳过 Shop Level....
简而言之,我想将“sub_cat_name”存储到 categoryArry 中。