Search code examples
ios7nsjsonserializationxcode5.1

iOS 7 : Getting JSON Response After Deserializing using NSDictionary in a Concatenated Way


After Receiving JSON Response From the Web-Service. I have got This Response from the Web service

{"d":"{\"Status\":0,\"Message\":\"Registered Successfully\",\"Token\":\"281fecff-2d44-4530-bc24-03558f1ebda1\",\"CurrentTimestamp\":\"23-09-2014 09:58:39 +05:30\",\"ValidUntilTimespan\":\"23-09-2014 09:58:39 +05:30\"}"}

and i have Deserialize this String First by Removing "d:" and "\" to make it valid JSON for parsing.

NSString *responseData = [[NSString alloc]initWithData:receivedData encoding:NSUTF8StringEncoding];
responseData = [responseData stringByReplacingOccurrencesOfString:@" " withString:@""];
responseData = [responseData stringByReplacingOccurrencesOfString:@"\\" withString:@""];
responseData = [responseData stringByReplacingOccurrencesOfString:@"\"d\":" withString:@""];
responseData = [responseData substringToIndex:[responseData length] - 2];
responseData = [responseData substringWithRange:NSMakeRange(2, [responseData length]-2)];

NSLog(@"Reponse data %@",responseData);
NSData *jsonData = [responseData dataUsingEncoding:NSUTF8StringEncoding];
NSError *errorJson=nil;
NSDictionary *myDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&errorJson];
NSLog(@"Final %@",myDictionary);

NSString *valueMessage = [myDictionary objectForKey:KeyMessage];
NSString *valueStatus = [myDictionary objectForKey:KeyStatus];
NSString *valueCurrentTimestamp = [myDictionary objectForKey:KeyCurrentTimestamp];
NSString *valueToken = [myDictionary objectForKey:KeyToken];
NSString *valueValidUntillTimepstamp = [myDictionary objectForKey:KeyValidUntillTimepstamp];
    //values
NSLog(@"Message = %@",valueMessage);
NSLog(@"Status = %@",valueStatus);
NSLog(@"currenttimestamp= %@",valueCurrentTimestamp);
NSLog(@"token= %@",valueToken);
NSLog(@"validuntiltimespan= %@",valueValidUntillTimepstamp);

This are my Log Results:

014-09-23 10:03:13.885 Sbits_Journal[1015:60b] Response data   {"Status":0,"Message":"RegisteredSuccessfully","Token":"281fecff-2d44-4530-bc24-0   3558f1ebda1","CurrentTimestamp":"23-09-201409:58:39+05:30","ValidUntilTimespan":"23-09-  201409:58:39+05:30"}
2014-09-23 10:03:13.886 Sbits_Journal[1015:60b] Final {
CurrentTimestamp = "23-09-201409:58:39+05:30";
Message = RegisteredSuccessfully;
Status = 0;
Token = "281fecff-2d44-4530-bc24-03558f1ebda1";
ValidUntilTimespan = "23-09-201409:58:39+05:30";
}

But i am Getting String response in concatenated way "RegisteredSuccessfully" Like This but i wanted to just fetch this string and Show Alert Since getting strings like this i am not able to show Like "Registered Successfully" in a Alert. Is it the Correct Way I have Parsed Or Is There Any another way of Deserilization? Thanks In Advance.


Solution

  • I have Figured Out the issue after Debugging Line by Line

    responseData = [responseData stringByReplacingOccurrencesOfString:@" " withString:@""];
    

    Due To this Line Space Was Getting Concatenated So i Just Comment Above Line and Run Again. I Know this was very Minor issue but may be it can Help Someone so i answered my own question.