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

时间:2015-04-01 09:10:23

标签: ios objective-c json nsarray nsdictionary

我使用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] );


            }

        }
    }
}

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

由于

字典输出

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

3 个答案:

答案 0 :(得分:1)

您不需要执行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"]);
        }
    }
}

答案 1 :(得分:1)

永远记住,当有{}花括号时,表示它是字典,当[]时,表示数组

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
}

答案 2 :(得分:1)

这是我的解析器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) {
        }];
    }];
}