2012-12-31 71 views
0

嗨我正在做一個iPhone應用程序,我在哪裏從JSON格式的Web服務響應。我正在使用SBJSON解析器來解析我的數據。iPhone:JSON解析與SBJSON解析器給出錯誤

我在嘗試解析數據時遇到錯誤。

我已經下文稱到以下鏈接但沒有人確實幫助:

How to handle '[<__NSCFString 0x2f1730> valueForUndefinedKey:]: this class is not key value coding-compliant for the key $oid' error

NSCFString 0x2749a0 valueForUndefinedKey this class is not key value coding-compliant for the key data

出了什麼問題?

任何幫助表示讚賞。

代碼:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"<MyWebserviceURL>"]]; 

    /* Following function is used to convert and concate json string with url and create query string to exicute to the server.*/ 
    [request addValue: @"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
    [request addValue: @"application/json; charset=utf-8" forHTTPHeaderField:@"Accept"]; 
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]]; 
    [request addValue: msgLength forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPMethod:@"POST"]; 
    [request setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; 

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse: nil error: nil]; 

    /* This string will convert all the data format to string format and store it.*/ 
    NSString *myString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
    NSLog(@"getresponse:%@",myString); 
    /* This string will used get json value and split the value starting from "d".*/ 
    //NSDictionary *d = [[myString JSONValue] objectForKey:@"d"]; 
    NSLog(@"[myString JSONValue]: %@",[[myString JSONValue] class]); 
    NSDictionary *d = [[myString JSONValue] valueForKey:@"d"]; // <----- ERROR IS SHOWN HERE 

控制檯日誌顯示:

getresponse:{"d":{"__type":"<CompanyName>.<AppName>.BusinessLogic.RegisterWebservice","ErrorObject":{"__type":"<CompanyName>.<AppName>.BusinessLogic.Error","ErrorMessage":"","ErrorCode":0},"AttendeeCredentials":{"__type":"<CompanyName>.<AppName>.BusinessLogic.AttendeeCredential","UserID":"abcd","Password":"abcd"},"ServerDeviceSN":"PKKPAPPKAPPKAPK090"}} 
    [myString JSONValue]: __NSCFString 

    *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0xc384f30> valueForUndefinedKey:]: this class is not key value coding-compliant for the key d.' 
*** First throw call stack: 
(0x2eaa012 0x2b1ae7e 0x2f32fb1 0x13aa5ed 0x13168db 0x131688d 0xf6aef 0xf64b9 0xfecef 0xfe834 0x13490d5 0x1349034 0x950d8ed9 0x950dc6de) 
libc++abi.dylib: terminate called throwing an exception 
+0

如果'SBJSON'壞了怎麼辦? JSON似乎很好。你爲什麼不使用'NSJSONSerialization'? – 2012-12-31 07:39:41

+0

可能是因爲json。獲取NSLog中的json並使用以下鏈接驗證它... –

+0

@Anil Kothari我已經驗證了它。問題出現在NSDictionary中* d = [[myString JSONValue] valueForKey:@「d」]; –

回答

2

您需要使用的NSDictionary方法-objectForKey:而非鍵 - 值編碼方法-valueForKey:。這些是而不是(總是)可以互換。