我曾经有来自服务器的 JSON 数据。它的格式如下:
(
{
comment = 1;
"comment_id" = 41;
"commenter_id" = 1;
date = "2013-04-02";
"first_name" = Alex;
"plan_id" = 27;
privacy = 0;
"solution_part" = 1;
}
)
我这样解析它:
-(void)loadTitleStrings
{
theArray = [NSMutableArray array];
if(!standardUserDefaults)
standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSLog(@"Arrayyy: %@", items_array);
NSString *is_private = [standardUserDefaults objectForKey:@"is_private"];
NSMutableArray *titleStrings = [NSMutableArray array];
for(NSDictionary *dictionary in items_array)
{
//NOT SURE WHAT THIS IS FOR, SEEMS WEIRD
NSString *tcid = [dictionary objectForKey:@"comment_id"];
[theArray addObject:tcid];
//NSLog(@"Arrayyy: %@", theArray);
NSString *string;
if(!is_private || [is_private isEqualToString:@"0"])
{
string = [NSString stringWithFormat:@"%@: %@", [dictionary objectForKey:@"first_name"], [dictionary objectForKey:@"comment"]];
}
else
{
string = [NSString stringWithFormat:@"%@", [dictionary objectForKey:@"comment"]];
}
[titleStrings addObject:string];
}
cellTitleArray = titleStrings;
}
但是现在服务器发生了变化,数据的格式如下:
{
data = (
{
comment = 1;
"comment_id" = 41;
"commenter_id" = 1;
date = "2013-04-02";
"first_name" = Alex;
"plan_id" = 27;
privacy = 0;
"solution_part" = 1;
},
{
comment = 2;
"comment_id" = 42;
"commenter_id" = 1;
date = "2013-04-02";
"first_name" = Alex;
"plan_id" = 27;
privacy = 0;
"solution_part" = 1;
}
);
并且解析它的原始代码崩溃了。我不确定格式差异到底是什么。我将如何更改原始代码以使用新的数据格式?
这是我得到的错误:
2013-04-02 12:31:28.798 Funding[70605:11303] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x72b9a10
2013-04-02 12:31:28.800 Funding[70605:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x72b9a10'
*** First throw call stack:
(0x1dd5012 0x126ce7e 0x1e604bd 0x1dc4bbc 0x1dc494e 0x14a52 0x1695b 0x17cb731 0x17da014 0x17ca7d5 0x1d7baf5 0x1d7af44 0x1d7ae1b 0x21687e3 0x2168668 0x1b0ffc 0x26dd 0x2605)
libc++abi.dylib: terminate called throwing an exception
谢谢,亚历克斯