JSON Parse iOS Array of Dictionaries 解析成数组只显示 tableview 中的最后一个对象
Posted
技术标签:
【中文标题】JSON Parse iOS Array of Dictionaries 解析成数组只显示 tableview 中的最后一个对象【英文标题】:JSON Parse iOS Array of Dictionaries parsed into array only displays last object in tableview 【发布时间】:2014-01-27 11:40:09 【问题描述】:我正在解析一个 JSON 字典数组。我的问题是我想在数组中显示所有符合我所需键值对(键是名称)的记录,我只在我的表格视图中显示最后一个。几乎是凌晨 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"
]
]
【问题讨论】:
请发布你的json 【参考方案1】:既然已经有了数组,为什么还要循环呢?只需使用 JSON 响应分配您的数组
NSArray *jsonDataArray = [[NSArray alloc] initWithArray:[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]];
这个数组将是你的数据源,所以从委托和你的 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"];
【讨论】:
起初我认为这个答案有效,但后来我意识到它仍然上升了一个级别,我需要在节目中使用名称键。 我希望值“Shark”和“Flipper”显示在我的 JSON 示例的表格中,您的代码给了我“名称 1”和“名称 2” 您可以更改您的密钥及其简单,我已经更新了我的答案,如果您想显示更多标签,然后创建一个自定义单元格并执行此操作【参考方案2】:从我的例子:
我的 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
这样的静态值。
【讨论】:
【参考方案3】: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.
希望对你有帮助
【讨论】:
【参考方案4】: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);
----------
【讨论】:
以上是关于JSON Parse iOS Array of Dictionaries 解析成数组只显示 tableview 中的最后一个对象的主要内容,如果未能解决你的问题,请参考以下文章
JSON parse error: Cannot deserialize instance of `int` out of START_OBJECT token; nested exception i
JSON.parse:unexpected end of data at line 1 column 1 of the json data
解析 json 错误:SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
JSON parse error: Cannot deserialize instance of `java.util.ArrayList
如何解决 SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data in ajax and p
jQuery解析JSON出现SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data(示例代码