json 解析都层数据
来源:互联网 发布:知豆电动汽车价位 编辑:程序博客网 时间:2024/06/07 09:36
- (IBAction)showMessage{ @try { NSError *error; //加载一个NSURL对象 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.1.111:8080/getIosJson"]]; //将请求的url数据放到NSData对象中 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中 NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; NSDictionary *weatherInfo = [weatherDic objectForKey:@"result"]; //反回结果 NSEnumerator *enumerator = [weatherInfo objectEnumerator]; id key2; while ((key2 = [enumerator nextObject])) { NSLog(@">>>>>>>>>@@@>>>%@", key2); NSString *wheatherString = [ NSString stringWithFormat:@"要发生的数据 %@ %@ ", [key2 objectForKey:@"movile"], [key2 objectForKey:@"text"] ]; NSLog(@"weatherInfo字典里面的内容为--》%@", wheatherString ); } NSArray *keys; int i, count; id key, value; keys=[weatherDic allKeys]; count=[keys count]; for (i = 0; i < count; i++) { key = [keys objectAtIndex: i]; value = [weatherDic objectForKey: key]; NSLog (@"Key: %@ for value: %@", key, value); } NSLog(@"weatherInfo字典里面的内容为--》%@", weatherDic ); }@catch (NSException *exception) { NSLog(@"%@ %@", [exception name], [exception reason]);}