我有一个我正在解析的JSON数组字典。我的问题是我想在数组中显示所有符合我所需的键值对(键是名称)的记录,我只在我的tableview中显示最后一个。现在差不多凌晨4点了,我从上午10点起就开始绕圈了,所以任何帮助都会非常感激,因为我忽略了一些东西。谢谢!
NSArray *jsonDataArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
self.someArray = [[NSMutableArray alloc]init];
for (NSMutableDictionary *dictionary in jsonDataArray)
{
NSMutableArray* Response_array = [dictionary objectForKey:@"loans"];
NSLog(@"Array: %@", Response_array);
NSDictionary *dict = [Response_array objectAtIndex:0];
NSLog(@"dict is %@",dict);
self.someMutableArray = [dict objectForKey:@"name"];
NSLog(@"what is name %@",self.someMutableArray);
}
我试过了:
NSArray *jsonDataArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
for ( int i = 0 ; i < jsonDataArray.count ; i++ )
{
self.someDictionary = [[jsonDataArray objectAtIndex:i] objectForKey:@"loans"];
self.someMutableArray = [self.someDictionary valueForKey:@"name"];
NSLog(@"MutableArray inside is %@", self.someMutableArray);
}
NSLog(@"MutableArray outside is %@",self.someMutableArray);
在我最后一次尝试时,我移动了valueForKey的位置:@“name”:
NSArray *jsonDataArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
self.someMutableArray = [NSMutableArray array];
for ( int i = 0 ; i < jsonDataArray.count ; i++ )
{
self.someMutableArray = [[jsonDataArray objectAtIndex:i] objectForKey:@"loans"];
}
NSLog(@"someMutableArray outside is %@",self.someMutableArray);
然后在我的tableview的cellForRowAtIndexPath中,我有:
NSDictionary *dict2 = [self.someMutableArray objectAtIndex:indexPath.row];
NSLog(@"what is dict2 %@", dict2);
cell.textLabel.text = [dict2 valueForKey:@"name"];
以下是我的JSON的设置:
[
-{
id: 285,
name: name 1,
-loans: [
-{
id: 42,
name: "Shark"
}
]
},
-{
id: 286,
name: name 2,
-loans: [
-{
id: 50,
name: "Flipper"
}
]
}
]
答案 0 :(得分:4)
为什么你在拥有数组时要进行循环?只需使用JSON响应分配您的数组
NSArray *jsonDataArray = [[NSArray alloc] initWithArray:[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]];
这个数组将是你的数据源,所以从delegate和你的rowAtIndexPath返回它的计数
NSDictionary *dictObject = [jsonDataArray objectAtIndex:indexPath.row];
cell.textLabel.text = [dictObject valueForKey:@"Shark"];
对于自定义单元格来说
NSDictionary *dictObject = [jsonDataArray objectAtIndex:indexPath.row];
cell.textLabel1.text = [dictObject valueForKey:@"Shark"];
cell.textLabel2.text = [dictObject valueForKey:@"Flipper"];
答案 1 :(得分:0)
从我的例子:
我的JSON文件:
{
status: "Success",
totalcount: "25",
currentcount: "25",
ticketreplies: [
{
ticket_id: "5348CD97",
subject: "bA==",
posteduser: "10025",
postedby: "Admin (Paul)",
message: "PGRpdj4",
post_id: "75",
posted_on: "23 Apr 2014 12:58 IST",
photo: "10025.jpg"
},
{
ticket_id: "5348F754",
subject: "Y2FsbCBv",
posteduser: "10025",
postedby: "Admin (John)",
message: "PGRpdj4",
post_id: "74",
posted_on: "23 Apr 2014 11:46 IST",
photo: "10025.jpg"
}
]
}
我的实施:
self.dic_response=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"Dictionary: %@",self.dic_response);
NSLog(@"Dictionary: %i",[self.dic_response count]);
NSString usrTicketId=[self.dic_response valueForKey:@"totalcount"];
NSLog(@"Ticket_Id: %@",usrTicketId);
NSArray arrTicketRplys=[self.dic_response valueForKey:@"ticketreplies"];
NSLog(@"Ticket_Id: %@",arrTicketRplys);
for(NSArray *tmpArr1 in arrTicketRplys){
if([[tmpArr1 valueForKey:@"posteduser"] isEqualToString:@"10025"]){
NSString *strPostUserName=[tmpArr1 valueForKey:@"postedby"];
NSLog(@"Posted BY: %@", strPostUserName);
}
}
//当然,它的逻辑适用于读取特定对象键和值。
//主要是,这些代码避免使用objectAtIndex:4
等静态值。
答案 2 :(得分:0)
self.dic_response=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
if([self.dic_response valueForKey:@"success"]){
NSMutableArray *resultArray = [NSMutableArray alloc];
for(NSDictionary *tmpArr1 in self.dic_response){
{
[resultArray addObject:tmpArr1];
//this resultArray have whole array value.
}
}
希望这有助于你
答案 3 :(得分:0)
for ( int i = 0 ; i <jsonDataArray.count ; i++ )
{
self.someDictionary = [[jsonDataArray objectAtIndex:i] objectForKey:@"loans"];
[self.someMutableArray addObject:[self.someDictionary valueForKey:@"name"]]
NSLog(@"MutableArray inside is %@", self.someMutableArray);
}
NSLog(@"MutableArray outside is %@",self.someMutableArray);
----------