2015-07-20 53 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];