JSON parsing error in iOS
I am trying to parse the json response (which I get from the SOAP web service response result tag) as shown in the pictures with the following lines.
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"]);
}
But I am getting zero in the json object. Here is a paste link for the JSON response http://pastie.org/9799331 . Below is a description of the error pointer.
Print errorPointer description: Domain error = NSCocoaErrorDomain Code = 3840 "The operation could not be completed. (Cocoa error 3840.)" (JSON text did not start with an array or object, and the option allowed fragments to be deleted.) UserInfo = 0x7d0156a0 {NSDebugDescription = JSON text did not start with an array or object and an option to not create slices.}
source to share
Is this your own web service or is it an existing one? If it's your own, are you sure your JSON is actually correct?
I believe the problem is here:
"ImageBase64Data": "/9j/4AAQSkZJRgABAQA .... blah blah ==
"
Pay attention to the key point here. That the trailing double quote is on the next line. It should be
"ImageBase64Data": "/9j/4AAQSkZJRgABAQA .... blah blah =="
For future reference, it is much easier if you cut and paste the JSON payload text in your question versus showing an image.
So, grabbing your data here:
http://pastie.org/9799331#2,22,482
I ran it through this code:
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);
Note that I have one line commented out right now. When I do this, it fails. However, the disclaimer is different from what you stated above. I get.
The error is a domain error = NSCocoaErrorDomain Code = 3840 "Operation failed to complete. (Cocoa error 3840.)" (Unescaped control character around character 120.) UserInfo = 0x7f9519f71510 {NSDebugDescription = Unhandled control character around character 120.}
If I uncomment this line it works. Only a fragment of the output is shown:
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/
Your data uses base64 output, which uses newlines to delimit each encoded string. That's why you see text at the end that looks like this:
==
","ImageName"
You will also notice that the vast majority of Base64 lines look consistent ... due to this limitation.
Extract newlines using this (you need to change it for code)
jsonStr = [[jsonStr componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@""];
If that doesn't work, you need to provide additional information:
-I output from Xcode console? -Add in your code where NSLog exists to get the result (if you don't have it, add it). - When you add NSLog, indicate that the output
While it may seem like you've already provided this information, there is obviously a gap in the information. Otherwise, I expected to see the same NSError output. So we have either skipped steps or maybe there are other deviations. For example, I am running this in the iOS 8 simulator. Not that it is too important, but there is always the possibility that you are running an older version of iOS that parses JSON differently.
source to share