本文介绍了如何防止NSJSONSerialization在URL中添加额外的转义符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何防止NSJSONSerialization向URL字符串添加多余的反斜杠?

How do I prevent NSJSONSerialization from adding extra backslashes to my URL strings?

NSDictionary *info = @{@"myURL":@"http://www.example.com/test"};
NSData data = [NSJSONSerialization dataWithJSONObject:info options:0 error:NULL];
NSString *string = [[NSString alloc] initWithData:policyData encoding:NSUTF8StringEncoding];
NSLog(@"%@", string);//{"myURL":"http:\/\/www.example.com\/test"}

我可以去除反斜杠并使用该字符串,但是如果可能的话,我想跳过该步骤...

I can strip the backslashes and use that string but I would like to skip that step if possible...

推荐答案

是的,这很烦人,甚至更多,因为似乎没有针对此问题的快速"解决方案(例如,用于NSJSONSerialization)

Yeah, this is quite irritating and even more so because it seems there's no "quick" fix to this (i.e. for NSJSONSerialization)

来源:
http://www.blogosfera.co.uk/2013/04/nsjsonserialization-serialization-of-a-string- Containing-forward-slashes-and-html-is-e-escaped-incorrectly/

包含正斜杠的字符串的NSJSONSerialization序列化/HTML是

source:
http://www.blogosfera.co.uk/2013/04/nsjsonserialization-serialization-of-a-string-containing-forward-slashes-and-html-is-escaped-incorrectly/
or
NSJSONSerialization serialization of a string containing forward slashes / and HTML is escaped incorrectly

(这里只是在黑暗中射击,所以请多多包涵我)
如果要创建自己的JSON,则只需从字符串中创建一个NSData对象,然后将其发送到服务器.
无需通过NSJSONSerialization.

(just shooting in the dark here so bear with me)
If, you're making your own JSON then simply make an NSData object out of a string and send it to the server.
No need to go via NSJSONSerialization.

类似的东西:

NSString *strPolicy = [info description];
NSData *policyData = [strPolicy dataUsingEncoding:NSUTF8StringEncoding];

我知道这不会那么简单,但是...嗯...无论如何

i know it won't be so simple but... hm... anyways

这篇关于如何防止NSJSONSerialization在URL中添加额外的转义符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 00:56