NSMutableString to NSDictionary conversion returns null [On priority]

474 views Asked by At
NSMutableString * strSocketResponse = [[NSMutableString alloc]init];

 strSocketResponse = //Append socket response from server which is valid JSON string
//This response is without escaping character '\'

 NSString *jsonString = @"{\"ID\":{\"Content\":268,\"type\":\"text\"},\"ContractTemplateID\":{\"Content\":65,\"type\":\"text\"}}";

 NSDictionary *dictValues1 = [[NSDictionary alloc] init];
 NSDictionary *dictValues2 = [[NSDictionary alloc] init];

 NSData* data1 = [strSocketResponse dataUsingEncoding:NSUTF8StringEncoding];
 NSData* data2 = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

 NSError *error;

 dictValues1 = [NSJSONSerialization JSONObjectWithData:data1 options:kNilOptions error:&error];

 dictValues2 = [NSJSONSerialization JSONObjectWithData:data2 options:kNilOptions error:&error];

 NSLog("dictValues1 : %@", [dictValues1 description]);
 NSLog("dictValues2 : %@", [dictValues2 description]);

NSLog value of strSocketResponse :

       {
    "data": {
        "elapsed": "0.171s",
        "size": "10",               
         ...
        "items": [
            {
                "code": "29688",
                "length": "17",
                ...
             }, 
            {   ...
             }
          ]
      }
   }

The log for dictValues1 is always null where as for dictValues2 is correct. The only difference I found between two is that conversion string for dictValues1 is NSMutableString without escaping character '\'. It is not possible to add escape character '\' in string since it is too long.

How do I deal with this problem? Sure +1 for correct answer!!

0

There are 0 answers