2012-09-14 44 views
-4

JSON格式代碼看起來是這樣的:如何可以打印陣列JSON值插入的UITableView在iphone

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

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

解析JSON值以上代碼陣列返回後哪個陣列返回

{ 
      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]; 
} 
+0

什麼是'dz'?它是如何聲明的? –

+0

你甚至嘗試了谷歌搜索嗎?尋找一個json解析器或自己做。提示:這是一個'NSDictionary'。 – Novarg

+0

你已經完成了dz一個,但問題是如何使用forloop從nsdictionary獲取值到nsarray打印uitableview ????? – areddy

回答

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

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

+0

感謝您的快速回復請參考我編輯過的代碼並指導我? – areddy

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

在你的表格單元格現在把數組2的值。

像:

cell.text=[array2 objectAtIndex:indexPath.row];  
+0

thanq有價值的解決方案...但字典包含250個值...所以如何能把環路條件放在隔離區dzz? – areddy

0

返回類型爲[值valueForKey:@ 「費率」]是一個字典不值數組。現在

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; 
} 
+0

雅... thanq給予有價值的解決方案ashwin .. – areddy

+0

但上述代碼只返回一個值...但我的字典包含200個值...如何可以打印所有值,請幫助我?>? – areddy

+0

我可以回答你的上述問題,如果我可以看到收到價值的結構..你可以分享它。 –