-4

JSON格式代码如下所示:

{   
 "rates": {  
           "amc": "201",  
           "hyd": "500.50",  
           "guj": "200.10",  
           "afgd": "400"  
         } 
}                                                                  

解析JSON值后,上述代码数组返回:

array = [values valueForKey:@"rates"];

返回哪个数组

{  
           amc = "201"; 
           hyd = "500.50";  
           guj = "200.10";  
           afgd = "400";
           ...........etc



}

但我想在UITableView外观 amc:201中打印

我怎样才能做到这一点?

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
  [connection release];

   NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
   self.responseData = nil;

   values = [responseString JSONValue];
   array = [[NSMutableArray alloc] init];
   NSMutableArray *arrTitle = [[NSMutableArray alloc] init];
   NSMutableArray *arrValues = [[NSMutableArray alloc] init];
   array =[values valueForKey:@"rates"];

   NSLog(@"array values:--> %@",array);
 //    NSLog(@"values:--> %@",values);
 //    NSLog(@"Particular values:--> %@",[[values valueForKey:@"rates"] valueForKey:@"AED"]);

   tempDict1 = (NSMutableDictionary *)array;            
   NSArray *arr;// =[[NSArray alloc]init];
   arr = [[tempDict1 valueForKey:@"rates"] componentsSeparatedByString:@";"];
   NSLog(@"arr-->%@",arr);
   NSString *subStar = @"=";
   [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 objectForKey:@"AED"]);

   [array retain];
   [tbl_withData reloadData];
}
4

3 回答 3

1

尝试:

NSMutableArray * array2=[[NSMutableArray alloc]init];    
[array addObject: [[array objectAtIndex:0] objectForKey:@"amc"]];    
[array addObject: [[array objectAtIndex:0] objectForKey:@"hyd"]];    
[array addObject: [[array objectAtIndex:0] objectForKey:@"guj"]];    
[array addObject: [[array objectAtIndex:0] objectForKey:@"afgd"]];    

现在将 array2 值放入表格单元格中。

喜欢:

cell.text=[array2 objectAtIndex:indexPath.row];    
于 2012-09-14T06:01:33.670 回答
0

[values valueForKey:@"rates"] 的返回类型是字典而不是值数组。

NSDictionary *dict = [values valueForKey:@"rates"];

现在您可以使用objectForKey在字典中引用获取

或者,如果你想存储在数组中。

NSMutableArray *array = [NSMutableArray array];
for (NSString *key in [dict allKeys])
{
    array = [dict objectForKey:key];
}

所以最后数组具有字典中的所有值。

// returns no of rows
-(NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section  
{
    return [array count];
}

// contents of row will be    
-(UITableViewCell*) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath     
{
     static NSString *CellIdentifier = @"test";

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil) 
     {
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
     }

     // Configure the cell...
     NSString *value = [self. array objectAtIndex:indexPath.row];

     cell.textLabel.text = value;

     return cell;
}
于 2012-09-14T06:06:17.990 回答
0

尝试这个:-

 NSDictionary *dict =[values valueForKey:@"rates"];
 NSString *str=[dict valueForKey:@"amc"];

You need to do this:-
 NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
   self.responseData = nil;

   values = [responseString JSONValue];
   NSMutableArray *arrTitle = [[NSMutableArray alloc] init];
   NSMutableArray *arrValues = [[NSMutableArray alloc] init];
     NSDictionary *dict =[values valueForKey:@"rates"];
 NSString *str=[dict valueForKey:@"amc"];

休息你可以根据你的要求去做。

于 2012-09-14T05:57:35.697 回答