-1

I Have got this Response from WebService

{"d":"{"token":"b502645e-837f-4237-a6ff-d4323f2799dd","timestamp":"09/11/20147:46:43PM"}"}

I want to Parse this String so that i can get output like : token = b502645e-837f-4237-a6ff-d4323f2799dd timestamp = 09/11/20147:46:43PM So that i can Store it into Database.

This is my Code

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// The request is complete and data has been received
// You can parse the stuff in your instance variable now
/*
NSError *errorJson=nil;
NSString* OuterDict = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&errorJson];
NSLog(@"Outer Dictionary %@",OuterDict);
 */
NSString *responseData = [[NSString alloc]initWithData:receivedData encoding:NSUTF8StringEncoding];

responseData = [responseData stringByReplacingOccurrencesOfString:@" " withString:@""];
responseData = [responseData stringByReplacingOccurrencesOfString:@"\\" withString:@""];
//NSString* encodedString = [responseData stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//NSLog(@"%@",encodedString);
NSLog(@"Reponse data %@",responseData);
NSError *errorJson=nil;

NSData *jsonData = [responseData dataUsingEncoding:NSUTF8StringEncoding];
jsonData = [jsonData subdataWithRange:NSMakeRange(0, [jsonData length] - 1)];
NSDictionary* OuterDict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&errorJson];
NSLog(@"Outer Dict %@",OuterDict);

}

I am getting null as Output: Outer Dict (null)

Can Anyone Help me With this. Thanks In Advance.

Larme
  • 24,190
  • 6
  • 51
  • 81
Asmi237
  • 33
  • 8
  • Your JSON seems weird... It doesn't seems correct. For me, it has two wrong `"` (one before `{` and the other after `"`). – Larme Sep 12 '14 at 06:37
  • possible duplicate of [How can you deserialize an escaped JSON string with NSJSONSerialization?](http://stackoverflow.com/questions/16948427/how-can-you-deserialize-an-escaped-json-string-with-nsjsonserialization) – memmons Sep 12 '14 at 07:06
  • Thank you Larme Your Comment Helped alot..Actually Saved me. – Asmi237 Sep 12 '14 at 12:51

1 Answers1

1

Use following snippets at the top of your method. it will parse your JSON data and returns dictionary. You can perform any operations on Dictionary according to your need.

NSDictionary *dictResponse = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingAllowFragments error:&err];
NSLog(@"Response : %@", dictResponse);

Happy coding :)

Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57