gpt4 book ai didi

ios - iOS 中的 JSON 解析错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:37:24 24 4
gpt4 key购买 nike

我正在尝试解析 json 响应(我从 SOAP 网络服务响应的结果标记中获取),如以下几行图片所示。

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的打印说明:Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn't be completed. (Cocoa error 3840.)"(JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x7d0156a0 {NSDebugDescription=JSON 文本没有以数组或对象和允许未设置片段的选项开头。

最佳答案

这是您自己的网络服务还是现有的网络服务?如果这是你自己的,你确定你的 JSON 实际上是正确的吗?

我认为问题出在这里:

"ImageBase64Data": "/9j/4AAQSkZJRgABAQA .... blah blah ==
"

注意这里的重点。结束双引号在下一行。应该是

"ImageBase64Data": "/9j/4AAQSkZJRgABAQA .... blah blah =="

为了将来引用,如果您在问题中剪切并粘贴 JSON 有效负载文本比显示图像要容易得多。

所以从这里获取你的数据:

http://pastie.org/9799331#2,22,482

我已经通过这段代码运行了它:

NSError *error = nil;
NSString *path = [[NSBundle mainBundle] pathForResource:@"badjson" ofType:@"txt"];
NSString *jsonStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

// jsonStr = [[jsonStr componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@""];

NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];

id json = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];

NSLog(@"Error is %@", error);
NSLog(@"JSON is %@", json);

请注意,我现在有一行注释掉了。当我这样做时,它失败了。但是失败与上面列出的不同。我明白了。

Error is Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Unescaped control character around character 120.) UserInfo=0x7f9519f71510 {NSDebugDescription=Unescaped control character around character 120.}

如果我取消对该行的注释,那么它就可以工作。只显示输出的一个片段:

2014-12-26 12:03:35.020 Sample[49732:6379864] Error is (null)
2014-12-26 12:03:35.022 Sample[49732:6379864] JSON is {
Head = (
{
ID = 1092;
ImageBase64Data = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDACAWGBwYFCAcGhwkIiAmMFA0MCwsMGJGSjpQdGZ6eHJmcG6AkLicgIiuim5woNqirr7EztDOfJri8uDI8LjKzsb/

您的数据使用 base64 输出,它使用换行符来分隔每个编码行。这就是为什么您会在最后看到如下所示的文本:

==
","ImageName"

您还会注意到 Base64 中的绝大多数行看起来都是统一的.. 因为这种分隔。

使用它去掉换行符(你需要为你的代码修改它)

jsonStr = [[jsonStr componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@""];

如果这不起作用,那么您需要提供更多信息:

-输出是从 Xcode 控制台获取的吗?- 在您的代码中添加 NSLog 以获取输出(如果没有,请添加一个)。-添加 NSLog 后,提供该输出

虽然看起来您“已经提供了该信息”,但信息显然存在差距。否则我希望看到相同的 NSError 输出。所以我们要么缺少步骤,要么可能存在其他差异。例如,我在 iOS 8 模拟器中运行它。并不是说它应该太重要,但总是有可能你运行的是旧版本的 iOS,它以不同的方式解析 JSON。

关于ios - iOS 中的 JSON 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27627428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com