2015-07-20 52 views
-2

轉換如何刪除\\n。這兩個字是即將到來的。如何JSON格式

{ 
     PageNumber = 1; 
     businessCode = "{\n \"businessCode\" : \"[]\"\n}"; 
     businessName = ""; 
     categoryKeyword = "Ladies Traditional Wear Pagenumber"; 
     languageCode = en; 
     location = ""; 
     paginationSize = 5; 
    } 
+1

你是如何生成這個字符串的?它是從NSLog或類似的輸出嗎?這些是新的行字符和轉義字符,JSON解析器將忽略這些字符 – Paulw11

+0

我認爲您顯示錯誤的響應。 – user3182143

+0

請顯示代碼。 –

回答

0

可能低於答案是you.Follow這個

//just give your URL instead of my URL 
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"Your URL"]]; 

[request setHTTPMethod:@"GET"]; 

[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"]; 

NSError *err; 

NSURLResponse *response; 

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; 

//You need to check response.Once you get the response copy that and paste in ONLINE JSON VIEWER.If you do this clearly you can get the correct results.  

//After that it depends upon the json format whether it is DICTIONARY or ARRAY 

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err]; 

有用從上面的代碼中,你可以得到JSON.Try的正確格式這一個。

-1

NSMutableCharacterSet *charactersToRemove = [NSMutableCharacterSet new]; [charactersToRemove addCharactersInString:@"\n"]; [charactersToRemove addCharactersInString:@"\\"]; //This respresent obly one backslash, In Objective-c you have to support it with another slash [jsonString stringByTrimmingCharactersInSet:charactersToRemove];