I am banging my head against a wall and decided to check in with you guys.
I have the following JSON Data as a NSString:
{
"PATIENTID": "51853",
"MSWREFERRAL": " ",
"TIMEIN": " ",
"PRECAUTIONOTHERNOTE1": " ",
"AGE": "70",
"SAFETY2": " ",
"VISITTYPE": " ",
"RAMP": " ",
"BANNISTER": " ",
"CREATETIMESTAMP": "1308519326",
"SAFETY1": " ",
"AGENCYNAME": "RESPONSE HOME CARE",
"STAIRS": " ",
"AGENCYID": "415",
"GUID": "845A481E-8F54-4737-9F55-05FD10E3B931",
"ASSIST": " ",
"EVALDATE": "06/19/2011",
"PULMONARY": "NO",
"DOB": "01/17/1941",
"GENDER": " ",
"STATUS": "pending",
"SETTING": " ",
"CATHETER": "NO",
"PRECAUTIONOTHER1": "NO",
"RECENTEVENTS": "*test\n*newline",
"PTTIME": "",
"TYPE": "eval",
"PATIENTNAME": "Gloria Gordon",
"CARDIAC": "NO",
"PRECAUTION": "NO",
"VISITNUM": "1",
"PRECAUTIONOTHERNOTE2": " ",
"SHUNT": "NO",
"WB": "NO",
"FALL": "NO",
"PRECAUTIONOTHER2": "NO",
开发者_JAVA百科"LIVES": " ",
"THERAPIST": "Bernard George KATZ, PT",
"PAID": " ",
"PASTMEDHX": " "
}
I need to either remove the /n between *test and *newline or encode it with //n. I have tried everything I can think of and cannot get the string to replace.
So far I have tried:
replaceOccurrencesOfString:@"\n" withString:@""
stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet
It seems like for some reason, the formmating of the JSON is tripping up the NSString methods. More likely I am doing something stupid.
I appreciate your help.
-stringByTrimmingCharactersInSet:
only removes characters at the head and tail of the string it's sent to. It trims, it doesn't search and replace.
In this case, wouldn't the \n refer to the line breaks after each comma?
You may want to replace \\n instead (the literal characters instead of the line breaks)
replaceOccurrencesOfString:@"\\n" withString:@""
It's hard to know for sure what your problem is but I think you want to take the string: "*test\n*newline"
and replace it with "*test\\n*newline"
so that the character \
will be accepted by a JSON parser?
To do this:
NSString *str = @"*test\n*newline";
NSLog(@"First log %@",str);
str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
NSLog(@"Second log %@",str);
The output from this is as follows
First log = *test
*newline
Second log = *test\n*newline
Here you can see that in the first log you have a \n
which is printed as a new line. In the second log you have a \\n
which is printed as \n
as the escape character \
is escaped and therefore just printed.
精彩评论