亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

objective-c - iOS中使用NSSerialization把對(duì)象轉(zhuǎn)為JSON字符串后,多出來反斜杠的問題
迷茫
迷茫 2017-04-22 08:59:59
0
3
1009

代碼

   NSDictionary *dic = @{@"url": @"http://..."};                                                                                                                                             
   NSLog(@"%@", dic);
   NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
   NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
   NSLog(@"%@", jsonString);

執(zhí)行結(jié)果:

2014-06-12 14:44:19.427 main[64877:1322484] {                                                                                                                                              
     url = "http://...";                                                                                                                                                                    
 }                                                                                                                                                                                          
 2014-06-12 14:44:19.429 main[64877:1322484] {                                                                                                                                              
   "url" : "http:\/\/..."                                                                                                                                                                   
 }                         

轉(zhuǎn)換后的json字符串中url地址被轉(zhuǎn)義了 :(

使用字符串替換可以事后彌補(bǔ):

[jsonString stringByReplacingOccurrencesOfString:@"\\" withString:@""];

如何事先預(yù)防呢?

PS:在和UIWebView進(jìn)行js調(diào)用時(shí)需要不轉(zhuǎn)義的json字符串,所以還是希望正面解決掉。

標(biāo)題文字

迷茫
迷茫

業(yè)精于勤,荒于嬉;行成于思,毀于隨。

reply all(3)
Ty80

This does not need to be processed, just use it directly.
If it is taken out directly and displayed on the Label, it will be escaped internally. So don't bother.
You can refer to the blog I wrote "IOS 7 uses the system's own library to perform POST JSON asynchronous access operations"

PHPzhong

About 是否應(yīng)該轉(zhuǎn)義成/, 我并沒細(xì)究. 但是很多開源實(shí)現(xiàn) decode 的時(shí)候是不會(huì)將/轉(zhuǎn)回 in the json standard document. Therefore, I don’t agree with the point of view mentioned above. In some cases, it must be processed, but I don’t know the official NSJSONSerialization processing method, so I switched to the open source jsonkit to bypass this problem

伊謝爾倫

Apple is so capricious, please replace it manually, similar to:

NSDictionary *policy = ....;
NSData *policyData = [NSJSONSerialization dataWithJSONObject:policy options:kNilOptions error:&error];
if(!policyData && error){
    NSLog(@"Error creating JSON: %@", [error localizedDescription]);
    return;
}

//NSJSONSerialization converts a URL string from http://... to http:\/\/... remove the extra escapes
policyStr = [[NSString alloc] initWithData:policyData encoding:NSUTF8StringEncoding];
policyStr = [policyStr stringByReplacingOccurrencesOfString:@"\/" withString:@"/"];
policyData = [policyStr dataUsingEncoding:NSUTF8StringEncoding];

See:
how to prevent NSJSONSerialization from adding extra escapes in URL
NSJSONSerialization serialization of a string containing forward slashes / and HTML is escaped incorrectly

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template