【发布时间】:2015-02-22 00:18:11
【问题描述】:
我正在尝试解析 json 响应(我从 SOAP Web 服务响应的结果标记中获取),如下图所示。
NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>"
"<GetPhotoSession xmlns=\"http://tempuri.org/\">"
"<UserID>%@</UserID>"
"<photoSessionID>%@</photoSessionID>"
"</GetPhotoSession>"
"</soap:Body>"
"</soap:Envelope>",[HelperClass retrieveStringForKey:kUserID],self.currentSession.sessionID];
NSURL *url = [NSURL URLWithString:kBaseURL];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/GetPhotoSession" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection ) {
self.webResponseData = [NSMutableData data];
}else {
NSLog(@"Some error occurred in Connection");
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self.webResponseData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.webResponseData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Some error in your Connection. Please try again.");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"Received Bytes from server: %d", [self.webResponseData length]);
NSString *myXMLResponse = [[NSString alloc] initWithBytes: [self.webResponseData bytes] length:[self.webResponseData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",myXMLResponse);
NSError *errorPointer;
NSDictionary *dict = [XMLReader dictionaryForXMLString:myXMLResponse error:&errorPointer];
NSString *jsonData = dict[@"soap:Envelope"][@"soap:Body"][@"GetPhotoSessionResponse"][@"GetPhotoSessionResult"][@"text"];
NSData *data = [jsonData dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&errorPointer];
NSLog(@"%@",[json objectForKey:@"Head"]);
}
但我在“json”对象中得到了 nil。这是 JSON 响应 http://pastie.org/9799331 的粘贴链接。以下是错误指针的说明。
errorPointer的打印说明: 错误域 = NSCocoaErrorDomain 代码 = 3840“操作无法完成。(可可错误 3840。)”(JSON 文本未以数组或对象开头,并且允许未设置片段的选项。) UserInfo = 0x7d0156a0 {NSDebugDescription = JSON 文本没有以数组或对象开头,并且没有设置允许片段的选项。}
【问题讨论】:
-
将您的 JSON 粘贴到 JSONLint 并检查它是否有效
-
JSONLint 的结果:第 5 行解析错误:... "ImageBase64Data": "/9j/4AAQSkZJRgABAQA --------- --^ 期望 'STRING'、'NUMBER'、'NULL'、'TRUE'、'FALSE'、'{'、'['
-
ImageBase64Data键值无效,请检查,值之间是否有换行符或" -
@Rob No. 这是
标签的值,它是一个 JSON 响应 -
好的。我之所以追求这一点,是因为您报告的错误消息(“JSON 文本不是以数组或对象开头 ...”)与传递给
NSJSONSerialization的非 JSON 更一致,而不是 Midhun/Zaph 所追求的,即它是 JSON,但格式不正确。通常格式错误的 JSON 会给出更具描述性的错误。您的错误表明了一个更根本的问题。我可能会建议NSLog或data(应该以十六进制显示),这样我们就可以确认没有奇怪的BOM 或类似的东西。只需向我们展示它的开始......
标签: ios json web-services soap