iPhone開發筆記(22)-JSONValue Failed. Error is : Unescaped control character的解決方法
阿新 • • 發佈:2019-01-10
在利用json-framework來實現json解析的過程時,會出現"-JSONValue Failed. Error is : Unescaped control character"的錯誤。這種問題一般是由轉義字元引起的,這些轉義字元可能是因為web服務編寫時造成的,也可能是其它原因導致的。解決這一問題的關鍵程式碼如下。
NSString *jsonString = [[request responseString] stringByReplacingOccurrencesOfString:@"\r" withString:@""]; jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""]; jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\s" withString:@""]; jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\t" withString:@""]; jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\v" withString:@""]; jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\f" withString:@""]; jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\b" withString:@""]; jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\a" withString:@""]; jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\e" withString:@""];
經過處理後的字串再呼叫方法[jsonString JSONValue]來進行解析。