iOS实现从服务器请求json数据并转化成NSDictionary

时间:2021-06-22 17:56:53

NSURL *url = [NSURL URLWithString:URL];

NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevaildatingCacheData timeoutInterval:10];

NSURLResponse *response;

NSError *error;

NSData *data = [NSURLConnection sendSynchronousRequest returningResponse:&response error:&errro];

 

NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

 

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

NSLog(@"%@\n %@",str,dic);