将NSDictionary转换为json字符串,将\替换为\/的问题 [英] Issue in converting NSDictionary to json string, replacing / with \/

查看:415
本文介绍了将NSDictionary转换为json字符串,将\替换为\/的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将NSDictionary转换为json字符串.一切工作正常,我有一个小问题,描述如下: 我有以下代码将NSDictionary转换为NSString:

i want to convert NSDictionary to json string.everything is working fine, i have a small issue that is described as follows: I have a following code for conversion of NSDictionary to NSString:

-(NSString *)dictToJson:(NSDictionary *)dict
{
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
   return  [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];
}

我将该方法称为:

NSLog(@"%@", [self dictToJson:@{@"hello" : @"21/11/2014 10:07:42 AM"}]);

以下是此NSLog的输出:

following is the output of this NSLog:

{
  "hello" : "21\/11\/2014 10:07:42 AM"
}

我希望获得以下输出,我该如何实现:

I am expecting following output, how can i achieve it:

{
      "hello" : "21/11/2014 10:07:42 AM"
}

可以通过使用stringByReplacingOccurrencesOfString方法来完成,但是我不希望使用此方法.还有其他方法可以实现相同目标吗?

it can be done by using stringByReplacingOccurrencesOfString method, but i don't want this to use. is there any other way to achieve the same?

推荐答案

将JSON对象转换为String会转义正斜杠.这就是为什么在结果中添加反斜杠的原因.

Converting JSON object to String will escape the forward slash. That is why the back slash is added in your result.

如果将字符串转换回JSON对象并记录了该对象,则可以看到预期的结果.因此,您可以验证字符串没有问题.

If you convert the string back to JSON object and logged the object, you can see the result as expected. Thus you can verify, there is nothing wrong with the string.

这篇关于将NSDictionary转换为json字符串,将\替换为\/的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆