iPhone:SBJSON解析器解析JSON时出错
您好,我正在制作一个iPhone应用程序,在这个应用程序中,我从webservice获得JSON格式的响应。我正在使用SBJSON解析器解析我的数据 我尝试解析数据时出错 我参考了以下链接,但没有一个有帮助: 有什么不对劲吗 感谢您的帮助 代码:iPhone:SBJSON解析器解析JSON时出错,iphone,json,nsdictionary,sbjson,Iphone,Json,Nsdictionary,Sbjson,您好,我正在制作一个iPhone应用程序,在这个应用程序中,我从webservice获得JSON格式的响应。我正在使用SBJSON解析器解析我的数据 我尝试解析数据时出错 我参考了以下链接,但没有一个有帮助: 有什么不对劲吗 感谢您的帮助 代码: NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"<MyWebserviceURL>"]]
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
NSMutableURLRequest*request=[[NSMutableURLRequest alloc]initWithURL:[nsurlurlwithstring:@”“];
/*下面的函数用于转换json字符串并将其与url关联,以及创建查询字符串以退出到服务器*/
[RequestAddValue:@“application/json;charset=utf-8”forHTTPHeaderField:@“Content Type”];
[request addValue:@“application/json;charset=utf-8”forHTTPHeaderField:@“Accept”];
NSString*msgLength=[NSString stringWithFormat:@“%d”,[soapMessage length]];
[请求添加值:msgLength forHTTPHeaderField:@“内容长度”];
[请求设置HttpMethod:@“POST”];
[请求setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding];
NSData*returnData=[NSURLConnection sendSynchronousRequest:request returningResponse:nil错误:nil];
/*此字符串将所有数据格式转换为字符串格式并存储*/
NSString*myString=[[NSString alloc]initWithData:returnData编码:NSUTF8StringEncoding];
NSLog(@“getresponse:%@”,myString);
/*该字符串将用于获取json值,并从“d”开始拆分该值*/
//NSDictionary*d=[[myString JSONValue]objectForKey:@“d”];
NSLog(@“[myString JSONValue]:%@”,[[myString JSONValue]类];
NSDictionary*d=[[myString JSONValue]valueForKey:@“d”];// 您需要使用NSDictionary方法-objectForKey:
,而不是键值编码方法-valueForKey:
。这些不(总是)可互换。如果SBJSON
坏了怎么办?JSON看起来不错。为什么不使用NSJSONSerialization
?可能是因为json。在NSLog中获取json并使用以下链接验证它…@Anil Kothari我已经验证了它。问题出现在NSDictionary*d=[[myString JSONValue]valueForKey:@“d”];
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