http://xtremeinspection.com/new2you4kids/app/android/get_category_brand.php
以上是JSON的网址。
我想在xcode上显示“PARENT_CATEGORY_ID”=“0”的数据。
我使用了以下编码:
-(void) loadJSON
{
loadJSONData = [NSURL URLWithString:mainURL];
NSLog(@"Fetch JSON URL : %@",loadJSONData);
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:
loadJSONData];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
});
}
- (void)fetchedData:(NSData *)responseData
{
NSError* error;
json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
adultJSON = [json objectForKey:@"category"];
NSLog(@"Data: %@", adultJSON);
NSDictionary *typeData = [json objectForKey:@"category"];
categTypeArray = [[NSMutableArray alloc] initWithCapacity:0];
for (NSDictionary *types in typeData)
{
if([[NSString stringWithFormat:@"%@",[adultJSON valueForKey:@"PARENT_CATEGORY_ID"]] isEqualToString:@"0"])
{
[categTypeArray addObject:[types valueForKey:@"PARENT_CATEGORY_ID"]];
}
}
NSLog(@"Data: %@", adultJSON);
NSLog(@"Category Data: %@", categTypeArray);
}
但categTypeArray返回null。
我应该使用哪种方法?
答案 0 :(得分:2)
错误在于以下代码:
if([[NSString stringWithFormat:@"%@",[adultJSON valueForKey:@"PARENT_CATEGORY_ID"]] isEqualToString:@"0"])
你写了adultJSON
而不是types
。
将该代码更改为:
if([[NSString stringWithFormat:@"%@",[types valueForKey:@"PARENT_CATEGORY_ID"]] isEqualToString:@"0"])