将 NSDictionary 转换为 json 字符串的问题,将 / 替换为 \/

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27054352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:38:36  来源:igfitidea点击:

Issue in converting NSDictionary to json string, replacing / with \/

objective-cjsonnsjsonserialization

提问by Faisal Ikwal

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:

我想将 NSDictionary 转换为 json 字符串。一切正常,我有一个小问题,描述如下:我有以下代码用于将 NSDictionary 转换为 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];
}

I am calling the method as:

我将该方法称为:

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

following is the output of this NSLog:

以下是此 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"
}

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

它可以通过使用stringByReplacingOccurrencesOfString方法来完成,但我不想使用它。有没有其他方法可以实现相同的目标?

采纳答案by Jatin Rathod

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

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

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.

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

回答by rahulchona

Nsdictionary - to - string

Nsdictionary - 到 - 字符串

NSError * err;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:response options:0 error:&err];
NSString * myString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

string - to - NSDictionary

字符串 - 到 - NSDictionary

NSError * err;
NSDictionary * response = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:[NSData dataFromString:str] options:NSJSONReadingMutableContainers error:&err];

回答by Alex Andrews

Try this,

尝试这个,

NSData *json = [NSJSONSerialization dataWithJSONObject:dict
                                               options:0
                                                 error:&error];
NSString *jsonString = [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding];
// This will be the json string in the preferred format 
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\/" withString:@"/"];

// And this will be the json data object 
NSData *processedData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

回答by Jatin Rathod

Convert to response to Data with option PrettyPrinted and then convert Data to string using utf8 encoding Code :

使用选项 PrettyPrinted 转换为响应数据,然后使用 utf8 编码代码将数据转换为字符串:

    NSDictionary *dictResponse = responseObject[0];

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictResponse options:NSJSONWritingPrettyPrinted error:nil];


    NSString * myString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

回答by Sport

add this

添加这个

 -(NSString*) bv_jsonStringWithPrettyPrint:(BOOL) prettyPrint;  




-(NSString *)dictToJson:(NSDictionary *)dict {

     NSData *jsonData = [NSJSONSerialization dataWithJSONObject: dict
                                                   options:(NSJSONWritingOptions)    (prettyPrint ? NSJSONWritingPrettyPrinted : 0)
                                                     error:&error];

     if (!jsonData) {
        NSLog(@"bv_jsonStringWithPrettyPrint: error: %@", error.localizedDescription);
        return @"{}";
     } else {
        return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
     } 

 }

refer this Generate JSON string from NSDictionary in iOS

请参阅此从 iOS 中的 NSDictionary 生成 JSON 字符串