2015-04-01 65 views
0

我使用php來獲取json數據,這裏是響應。如何在iOS中解析複雜的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; 
} 
+0

複雜嗎?你還沒有見過*複雜*! – 2015-04-01 12:27:51

回答

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"]); 
     } 
    } 
} 
+0

它給錯誤,請檢查更新的。我更新了錯誤日誌 – 2015-04-01 09:28:34

+0

@PremTomar更新了代碼。對於之前的崩潰感到抱歉。那是我的不好。 – 2015-04-01 09:29:56

+0

非常感謝@ iosDev82。有用。 – 2015-04-01 09:32:17

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 
} 
+0

在JSON中。當翻譯成NSArrays和NSDictionarys和NSLogged時,{}仍然意味着字典,但是'()'意味着數組。 – 2015-04-01 12:25:58

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) { 
     }]; 
    }]; 
}