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]);
strSocketResponse的NSLog值:
{
"data": {
"elapsed": "0.171s",
"size": "10",
...
"items": [
{
"code": "29688",
"length": "17",
...
},
{ ...
}
]
}
}
dictValues1的日志始终为空,而dictValues2的日志是正确的。我发现两者之间唯一的区别是 dictValues1的转换字符串是NSMutableString而没有转义字符' 。无法添加转义字符' \'在字符串中,因为它太长了。
我该如何处理这个问题?肯定+1正确答案!!