我使用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;
}
複雜嗎?你還沒有見過*複雜*! – 2015-04-01 12:27:51