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

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
              ];
}
于 2013-01-08T04:27:36.140 に答える
0

iOS 5以降を使用している場合は、これを使用できます

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

サードパーティの JSON パーサーを使用していると思いますが、おそらく SBJSONです。このキットを含めて、NSString_SBJsonParsingカテゴリのJSONValueメソッドを使用してください。

于 2013-01-08T04:32:12.620 に答える