简体   繁体   English

iPhone:使用SBJSON Parser进行JSON解析会出错

[英]iPhone: JSON Parsing with SBJSON Parser gives error

Hi I am making an iPhone app where in I am getting response from webservice in JSON format. 嗨,我正在制作一个iPhone应用程序,我正在从JSON格式的webservice获得响应。 I am using SBJSON Parser for parsing my data. 我正在使用SBJSON Parser来解析我的数据。

I am getting an error while I try parsing the data. 我在尝试解析数据时遇到错误。

I have reffered to following links but none of them did help: 我已经提到过以下链接,但没有一个帮助过:

How to handle '[<__NSCFString 0x2f1730> valueForUndefinedKey:]: this class is not key value coding-compliant for the key $oid' error 如何处理'[<__ NSCFString 0x2f1730> valueForUndefinedKey:]:此类不是键值$ oid错误的键值编码

NSCFString 0x2749a0 valueForUndefinedKey this class is not key value coding-compliant for the key data NSCFString 0x2749a0 valueForUndefinedKey此类不是密钥值的密钥值编码兼容的密钥数据

What could be wrong? 可能有什么不对?

Any help is appreciated. 任何帮助表示赞赏。

Code: 码:

 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

Console Log shows: 控制台日志显示:

    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

You need to use the NSDictionary method -objectForKey: rather than the Key-Value-Coding method -valueForKey: . 您需要使用NSDictionary方法-objectForKey:而不是Key-Value-Coding方法-valueForKey: . These are not (always) interchangeable. 这些不是 (总是)可互换的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM