-1

我正在做 JSON 解析,我想在UITableView.

为此,我试图将解析后的数据分配给NSMutableDictionarytoNSArray以显示在表格视图中,但数组返回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];

}
4

2 回答 2

0

尝试将 connectionDidFinishLoading 中的第四行编辑为

values = [responseString JSONFragments];
于 2012-09-14T13:53:10.547 回答
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);
}
于 2012-09-14T14:11:04.963 回答