2014-12-04 54 views
-2

我有以下信息的字符串:如何刪除字符

\"[{\"CodRTA\":\"1\",\"MenssRTA\":\"messaje error\",\"Resp\":\"\"}]\" 

,我需要刪除我試圖刪除如下字符\,但性格使用系統和留下的代碼接近行

NSString *filtered = [[[restConnection stringData] componentsSeparatedByString:@"\"] componentsJoinedByString:@""]; 

NSLog(@"filtrado: %@", filtered); 

誤差

Expected ']' in this part : componentsSeparatedByString:@"\"] 
+0

約不刪除它是什麼。並解析json。它提供了一個錯誤?我相信json解析器會處理這個問題。 – hasan83 2014-12-04 13:22:20

+0

是的..這是json格式的字符串。那麼爲什麼不用json將它反序列化? – DipakSonara 2014-12-04 13:23:20

+0

我想這樣使用JSON:json = [NSJSONSerialization JSONObjectWithData:string options:0 error:nil];但不總是工作結果爲空 – lycros 2014-12-04 13:27:18

回答

0

像成才這

string = [string stringByReplacingOccurrencesOfString:@"\\" withString:@""]; 
1

它看起來像JSON數據,而不是干擾到JSON,只是轉換JSON字符串NSData然後進入NSDictionaryNSArray

NSString *jsonString = @"[{\"CodRTA\":\"1\",\"MenssRTA\":\"messaje error\",\"Resp\":\"\"}]"; 
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; 
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
NSArray *array = [NSArray arrayWithArray:json]; 

現在,如果你做以下的NSLog聲明

NSLog(@"%@",[[json firstObject] objectForKey:@"CodRTA"]); 

結果將是另一個NSDictionary。

{ 
    CodRTA = 1; 
    MenssRTA = messaje error; 
    Resp = ""; 
} 

順便說一句,我格式化你的JSON響應,它的外觀像這樣,

enter image description here

+0

我正是這樣做,但json始終爲空 – lycros 2014-12-04 13:37:58

+0

我發現錯誤並且原始字符串是@「\」[{\「CodRTA \」:\「1 \」,\「MessageRTA \」:\「information \」,\「Resp \」:\「\」}] \「 「和\」字符誰在最後和開始是錯誤的原因,但又不像刪除。 – lycros 2014-12-04 14:21:03

0

使用此代碼

NSString *[email protected]"[{\"CodRTA\":\"1\",\"MenssRTA\":\"messaje error\",\"Resp\":\"\"}]"; 
     NSString *filtered = [[str componentsSeparatedByString:@"\\"] componentsJoinedByString:@""]; 

           NSLog(@"filtrado: %@", filtered);