【问题标题】:iPhone: JSON Parsing with SBJSON Parser gives erroriPhone:使用 SBJSON Parser 进行 JSON 解析会出错
【发布时间】:2023-03-15 02:07:01
【问题描述】:

您好,我正在制作一个 iPhone 应用程序,在该应用程序中,我从 Web 服务获得 JSON 格式的响应。我正在使用 SBJSON Parser 来解析我的数据。

我在尝试解析数据时遇到错误。

我参考了以下链接,但它们都没有帮助:

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

【问题讨论】:

  • 如果SBJSON 坏了怎么办? JSON 看起来不错。为什么不用NSJSONSerialization
  • 可能是由于 json。获取 NSLog 中的 json 并使用以下链接进行验证...
  • @Anil Kothari 我已经验证过了。问题出在 NSDictionary *d = [[myString JSONValue] valueForKey:@"d"];

标签: iphone json nsdictionary sbjson


【解决方案1】:

你需要使用NSDictionary方法-objectForKey:而不是Key-Value-Coding方法-valueForKey:。这些是(总是)可以互换的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    相关资源
    最近更新 更多