我正在进行JSON解析,我想在UITableView
中显示解析后的数据。
为此,我尝试将解析后的数据从NSMutableDictionary
分配到NSArray
以显示在表视图中,但数组返回null
。
这里我的数组返回null
值;
NSMutableDictionary *tempDict1;
NSArray *arr = [[tempDict1 valueForKey:@"rates"] componentsSeparatedByString:@";"];
码
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;
// NSArray *latestrates = [[responseString JSONValue] objectForKey:@"rates"];
[responseString release];
values = [responseString JSONValue];
array = [[NSMutableArray alloc] init];
array = [values valueForKey:@"rates"];
NSLog(@"array values:--> %@",array);
tempDict1 = (NSMutableDictionary *)array;
arr = [[tempDict1 valueForKey:@"rates"] componentsSeparatedByString:@";"];
NSString *subStar = @"=";
NSMutableArray *arrTitle = [[NSMutableArray alloc] init];
NSMutableArray *arrValues = [[NSMutableArray alloc] init];
[arrTitle removeAllObjects];
[arrValues removeAllObjects];
for (int i=0; i<[arr count]-1; i++)
{
[arrTitle addObject:[[arr objectAtIndex:i] substringToIndex:NSMaxRange([[arr objectAtIndex:i] rangeOfString:subStar])-1]];
[arrValues addObject:[[arr objectAtIndex:i] substringFromIndex:NSMaxRange([[arr objectAtIndex:i] rangeOfString:subStar])]];
NSLog(@"arrTitle is:--> %@",arrTitle);
}
tempDict1 = (NSMutableDictionary*)[array objectAtIndex:0];
array = [values valueForKey:@"rates"];
NSLog(@"tempDict--%@",tempDict1);
[arr retain];
[tbl_withData reloadData];
}
答案 0 :(得分:0)
尝试在connectionDidFinishLoading中编辑第四行
values = [responseString JSONFragments];
答案 1 :(得分:0)
NSError *error = nil;
NSArray *array = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"Your data - %@",array);
现在您可以根据数据格式获取它。
修改强>
我想你也不知道如何获得webResponse
所以这是一种获得webResponse
的方法 -
首先在XML
课程中设置ViewController.h
代理人
并宣布NSMutableData
全球
@interface ViewController : UIViewController<NSXMLParserDelegate>
@property(nonatomic, retain)NSMutableData *responseData;
@end
现在在responseData
类
ViewController.m
@synthesize responseData = _responseData;
现在,您可以使用request
方法在服务器上发送viewDidLoad:
,以您想要发送的方式发送给您。
-(void)viewDidLoad
{
NSString *urlString = [NSString stringWithFormat:@"http://EnterYourURLHere"];
NSURL *URL = [NSURL URLWithString:urlString];
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]init];
[urlRequest setURL:URL];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];
NSURLConnection *urlConnection = [[NSURLConnection alloc]initWithRequest:urlRequest delegate:self];
if(!urlConnection)
{
[[[UIAlertView alloc]initWithTitle:@"OOoopppssS !!" message:@"There is an error occured. Please check your internet connection or try again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
}
}
#pragma mark - Parsing delegate methods
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.responseData = [[NSMutableData alloc]init];
[self.responseData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.responseData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//Now parse your data here -
NSError *error = nil;
NSArray *array = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"Your data - %@",array);
}