2013-10-17 28 views
-2

從web服務我得到的數據爲json,但問題是與json自帶的所有「與\ ie」所有「來作爲\」所有\「如何從目標c中的單詞「hello 」中移除?

如何使這個有效的json,然後字典?

{ 
    GetDataResult = "[{\"www\":{\"0\":{\"ID\":\"10233\",\"Queue\":\"COMPLETED\",\"EstCommName\":\"\U062e\U0631\U0645 \U0644\U0644\U0627\U0644\U0648\U0645\U0646\U064a\U0648\U0645 \U0648\U0627\U0644\U0632\U062c\U0627\U062c\",\"ReturnTime\":\"\",\"Latitude\":\"\",\"Longitude\":\"\"},\"1\":{\"ID\":\"10304\",\"Queue\":\"COMPLETED\",\"EstCommName\":\"\U0627\U062d\U0645\U062f \U0627\U0644\U0643\U0646\U062f\U064a \U0644\U0644\U0627\U0644\U0645\U0648\U0646\U064a\U0648\U0645 \U0648\U0627\U0644\U0632\U062c\U0627\U062c\",\"ReturnTime\":\"\",\"Latitude\":\"\",\"Longitude\":\"\"},\"2\":{\"ID\":\"10667\",\"Queue\":\"FRESH\",\"EstCommName\":\"\U0645\U0646\U062c\U0631\U0629 \U0627\U0644\U062e\U0632\U0646\U0629\",\"ReturnTime\":\"\",\"Latitude\":\"\",\"Longitude\":\"\"},\"3\":{\"ID\":\"10777\",\"Queue\":\"FRESH\",\"EstCommName\":\"\U0645\U0624\U0633\U0633\U0647 \U062c\U0647\U0627\U0645 \U0644\U0627\U0639\U0645\U0627\U0644 \U0627\U0644\U0633\U064a\U0631\U0627\U0645\U064a\U0643\",\"ReturnTime\":\"\",\"Latitude\":\"\",\"Longitude\":\"\"}}},{\"asd\":{}},{\"ssd\":{}}]"; 

換句話說

TLDR

如何從一個詞\ 「你好\」?即需要輸出爲 「你好」 刪除\。

我試圖

 NSLog(@"%@",[[op objectForKey:@"GetSampleDataResult"] stringByReplacingOccurrencesOfString:@"\"" withString:@""]); 
+3

你必須使用'NSJSONSerialization'爲'GetDataResult'也。 – Bhavin

+0

您正在替換「with」,「嘗試用」替換\「。」 即NSLog(@「%@」,[[op objectForKey:@「GetSampleDataResult」] stringByReplacingOccurrencesOfString:@「\\」withString:@「」] );' –

+0

什麼是「GetDataResult」?字符串文字?控制檯輸出?手工製作的東西? – CouchDeveloper

回答

1

我還沒有試過,但像這樣可以爲你工作。

示例代碼:

NSString *yourString = [yourJSON objectForKey:@"GetDataResult"]; 
NSData *data = [yourString dataUsingEncoding:NSUTF8StringEncoding]; 
NSError *error = nil; 
NSArray *www = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; 
NSLog(@"www :: %@",www); 
+0

那是不是轉換字符串的方式 –

+0

@ LithuT.V:更新了代碼。 – Bhavin

+1

是的,現在它的正確和完美 –

0

從我的頭頂,以取代\「你需要將其指定爲\\\」

原因是\本身被用作轉義字符,因此你需要逃避:

  • \以\\
  • 「與\」

但需要引號,所以你需要擺脫\
所以這次自己:

NSLog(@"%@",[[op objectForKey:@"GetSampleDataResult"] 
      stringByReplacingOccurrencesOfString:@"\\" 
      withString:@""]); 

但如前所述,NSJSONSerialization是去這

0
[string stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""]; 
的最佳方式

小心引號和反斜槓。

  • \\\"的結果將爲\",正是您需要找到的結果。
  • \"的結果將爲",正是您需要更換的結果。

編輯:這是回答「如何從一個字\"hello\"刪除\?」,而不是解決問題的辦法。

0

我想這樣的下面: -

NSString *word= @"'\'hello'\'"; 
    NSArray *arr=[word componentsSeparatedByString:@"'\'"]; 
    for (NSString *str in arr) 
    { 
     if ([str length] >0) 
     { 
     NSLog(@"%@",str); 
     } 
    } 
相關問題