-1

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];

}
4

2 に答える 2

0
NSError *error = nil;
NSArray *array = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"Your data - %@",array);

これで、データ形式に従って取得できます。

編集

を取得する方法も知らないと思いますwebResponse
したがって、ここに取得する方法がありますwebResponse-最初にクラスでXMLデリゲートを設定し、グローバルを宣言しますViewController.hNSMutableData

@interface ViewController : UIViewController<NSXMLParserDelegate>
@property(nonatomic, retain)NSMutableData  *responseData;
@end

responseDataこれをViewController.mクラスで合成しました

@synthesize responseData = _responseData;

これで、どのメソッドで送信するかはあなた次第ですrequestviewDidLoad:

-(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 に答える
0

connectionDidFinishLoading の 4 行目を編集してみてください

values = [responseString JSONFragments];
于 2012-09-14T13:53:10.547 に答える