如何在iphone中将数组json值打印到uitableview中
JSON格式代码如下所示:如何在iphone中将数组json值打印到uitableview中,iphone,ios,json,Iphone,Ios,Json,JSON格式代码如下所示: { "rates": { "amc": "201", "hyd": "500.50", "guj": "200.10", "afgd": "400" } } 解析JSON值后,上述代码数组返回: arr
{
"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
look 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];
}
-(void)连接dFinishLoading:(NSURLConnection*)连接
{
[连接释放];
NSString*responseString=[[NSString alloc]initWithData:responseData编码:NSUTF8StringEncoding];
自响应数据=零;
值=[responseString JSONValue];
array=[[NSMutableArray alloc]init];
NSMutableArray*arrTitle=[[NSMutableArray alloc]init];
NSMutableArray*arrValues=[[NSMutableArray alloc]init];
数组=[values valueForKey:@“rates”];
NSLog(@“数组值:-->%@”,数组);
//NSLog(@“值:-->%@”,值);
//NSLog(@“特定值:-->%@,[[values valueForKey:@“rates”]valueForKey:@“AED”]);
tempDict1=(NSMutableDictionary*)数组;
NSArray*arr;//=[[NSArray alloc]init];
arr=[[tempDict1 valueForKey:@“rates”]由字符串分隔的组件:@;“];
NSLog(@“arr-->%@),arr);
NSString*subStar=@“=”;
[arrTitle removeAllObjects];
[arrValues removeAllObjects];
对于(int i=0;i请尝试以下方法:-
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"];
您可以根据自己的要求进行休息。试试:
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];
[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;
}
什么是dz
?它是如何声明的?你甚至试过谷歌搜索吗?找一个json解析器或者自己做。提示:这是一个NSDictionary
。你已经做了dz-one,但问题是如何使用forloop从NSDictionary到nsarray获取值来打印uitableview?????thanq以获得有价值的解决方案…但dictionary包含250个值。。。那么,如何在standoff dzz中设置循环条件???但上面的代码只返回一个值…但我的字典包含200个值…如何打印所有值请帮助我?>??如果我能看到接收值的结构,我可以回答你的上述问题。你能分享它吗。是的,你可以看到接收值的结构显示为above of the code.array=[dict objectForKey:key];我打印的数组值是5.693399,但我希望在uitableview上输出,如amc=5.693399。您可以使用默认的tableview单元格/自定义