我是iphone开发的新手。 在这里我遇到执行代码的问题
-[__NSCFString JSONValue]: unrecognized selector sent to instance 0x78e1000
2013-01-08 09:43:21.194 loanjson[655:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString JSONValue]: unrecognized selector sent to instance 0x78e1000'
*** First throw call stack:
(0x1c8d012 0x10cae7e 0x1d184bd 0x1c7cbbc 0x1c7c94e 0x2b34 0xbd0e59 0xbcef22 0xbd016a 0xbceedd 0xbcf055 0xb1c338 0x460aa81 0x4609d33 0x4647e3a 0x1c2f8fd 0x46484bc 0x4648435 0x45323a0 0x1c10f3f 0x1c1096f 0x1c33734 0x1c32f44 0x1c32e1b 0x1be77e3 0x1be7668 0x1265c 0x2322 0x2255)
libc++abi.dylib: terminate called throwing an exception
我不知道为什么会出现这个错误,我正在使用JSON请求和响应,我已经检查了所有要在NSString中解析的JSON值 这是我的.m文件中的代码
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;
NSArray* latestLoans = [(NSDictionary*)[responseString JSONValue] objectForKey:@"loans"];
[responseString release];
NSLog(@"%@",latestLoans);
//choose a random loan
NSDictionary* loan = [latestLoans objectAtIndex:0];
//fetch the data
NSNumber* fundedAmount = [loan objectForKey:@"funded_amount"];
NSNumber* loanAmount = [loan objectForKey:@"loan_amount"];
float outstandingAmount = [loanAmount floatValue] - [fundedAmount floatValue];
NSString* name = [loan objectForKey:@"name"];
NSLog(@"====:%@",(NSDictionary*)[loan objectForKey:@"location"]);
NSString* country = [(NSDictionary*)[loan objectForKey:@"location"] objectForKey:@"country"];
//set the text to the label
label.text = [NSString stringWithFormat:@"Latest loan: %@ from %@ needs another $%.2f, please help",
name,country,outstandingAmount
];
}
请指导我, 感谢
答案 0 :(得分:0)
NSString
没有名为JSONValue
的方法。这就是它抛出无法识别的选择器的原因。您可以使用第三方Json框架(如Json Kit)来解析json。
如果使用SBJson Framewrok,请尝试以这种方式解析!
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *jsonArray = (NSDictionary *) [parser objectWithString:responseString error:nil];
NSArray *latestLoans = [jsonArray valueForKey:@"loans"];
[responseString release];
[parser release];
NSLog(@"%@",latestLoans);
//choose a random loan
NSDictionary* loan = [latestLoans objectAtIndex:0];
//fetch the data
NSNumber* fundedAmount = [loan objectForKey:@"funded_amount"];
NSNumber* loanAmount = [loan objectForKey:@"loan_amount"];
float outstandingAmount = [loanAmount floatValue] - [fundedAmount floatValue];
NSString* name = [loan objectForKey:@"name"];
NSLog(@"====:%@",(NSDictionary*)[loan objectForKey:@"location"]);
NSString* country = [(NSDictionary*)[loan objectForKey:@"location"] objectForKey:@"country"];
//set the text to the label
label.text = [NSString stringWithFormat:@"Latest loan: %@ from %@ needs another $%.2f, please help",
name,country,outstandingAmount
];
}
答案 1 :(得分:0)
如果您使用iOS 5
或更高版本,则可以使用此
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSArray* latestLoans = [json objectForKey:@"loans"]
我认为您使用的是第三方JSON解析器可能是SBJSON。请添加此工具包并使用 NSString_SBJsonParsing 类别的JSONValue
方法。