如何解析 iOS 中复杂的 JSON 数据

how to Parse complicated JSON data in iOS

我正在使用 php 获取 json 数据,这是响应。

{"array":[{"Category":{"charity_id":"2","charity_name":"GiveDirectly","charity_amt":"0.20"}}]}

这是我的 Objective-c 代码

NSError *err;
NSURL *url=[NSURL URLWithString:@"url"];
NSURLRequest *req=[NSURLRequest requestWithURL:url];
NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:&err];

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err];

if ([json isKindOfClass:[NSDictionary class]]){ 
    NSArray *yourStaffDictionaryArray = json[@"array"];
    if ([yourStaffDictionaryArray isKindOfClass:[NSArray class]]){
        for (NSDictionary *dictionary in yourStaffDictionaryArray) {

            for(NSDictionary *dict in dictionary[@"Category"]){

                NSLog(@"%@",[[((NSString*)dict) componentsSeparatedByString:@"="] objectAtIndex:0] );


            }

        }
    }
}

但这只是 returns 名称而不是值。我在这个网站上搜索了大部分问题,但没有任何帮助。请帮助我,我是 iOS.

的新手

谢谢

词典输出为

{
"charity_amt" = "0.20";
"charity_id" = 2;
"charity_name" = GiveDirectly;
}

您不需要执行 ComponentSeparatedByString。一旦你得到了@"Category"的NSDictionary,你就可以通过使用它的键来得到它的值。

类似于

if ([json isKindOfClass:[NSDictionary class]]){
    NSArray *yourStaffDictionaryArray = json[@"array"];
    if ([yourStaffDictionaryArray isKindOfClass:[NSArray class]]){
        for (NSDictionary *dictionary in yourStaffDictionaryArray) {

            NSDictionary *dict = dictionary[@"Category"];
            NSLog(@"%@",dict[@"charity_id"]);
        }
    }
}

Always Remember that when there's being { } curly brackets, it means it is Dictionary and when [ ] this, means Array

NSURL *url=[NSURL URLWithString:@"Your JSON URL"];

NSData *data = [[NSData alloc] initWithContentsOfURL:url];

NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

NSArray *array = json[@"array"];

for(NSMutableDictionary *dic in array)
{
  NSLog(@"%@",dic[@"Category"][@"charity_id"]); // prints 2
  NSLog(@"%@",dic[@"Category"][@"charity_name"]); // GiveDirectly
  NSLog(@"%@",dic[@"Category"][@"charity_amt"]); // 0.20
}

这是我的解析器json数据,它是演示

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSString *urlString         = @"https://api.foursquare.com/v2/venues/search?categoryId=4bf58dd8d48988d1e0931735&client_id=TZM5LRSRF1QKX1M2PK13SLZXRXITT2GNMB1NN34ZE3PVTJKT&client_secret=250PUUO4N5P0ARWUJTN2KHSW5L31ZGFDITAUNFWVB5Q4WJWY&ll=37.33%2C-122.03&v=20140118";
    NSURL   *url                = [NSURL URLWithString:urlString];
    NSURLRequest *request       = [NSURLRequest requestWithURL:url];
//    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    NSOperationQueue *queue     = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                                           NSString *string = [[NSString alloc] initWithData:data
                                                                                    encoding:NSUTF8StringEncoding];
                                           [self parser:data];
                                       }];
}


- (void)parser:(NSData *)data
{
    NSMutableDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data
                                                                     options:0
                                                                       error:nil];
    [dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        [obj enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        }];
    }];
}