1

我是 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
                  ];
}

请指导我,谢谢

4

2 回答 2

0

如果您正在使用iOS 5或更高版本,那么您可以使用它

NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
 NSArray* latestLoans = [json objectForKey:@"loans"]

我认为您使用的 3rd 方 JSON 解析器可能是SBJSON。请包含此工具包并使用NSString_SBJsonParsing类别的JSONValue方法。

于 2013-01-08T04:32:12.620 回答
0

NSString没有名为 的方法JSONValue。这就是为什么它会抛出无法识别的选择器。您可以使用Json Kit等第三方 Json 框架来解析 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
              ];
}
于 2013-01-08T04:27:36.140 回答