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

5

9

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];
}

I am calling the method as:

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

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"
}

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?

Stanzel answered 21/11, 2014 at 4:53 Comment(1)
is there something wrong if you just call the description method on the dictionary?Snaggy
R
0

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

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.

Roughhouse answered 21/11, 2014 at 5:12 Comment(0)
W
22

Nsdictionary - to - string

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

string - to - NSDictionary

NSError * err;
NSDictionary * response = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:[NSData dataFromString:str] options:NSJSONReadingMutableContainers error:&err];
Willawillabella answered 4/4, 2015 at 13:57 Comment(0)
A
2

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];
Amaranth answered 21/11, 2014 at 6:6 Comment(0)
F
1

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

    NSDictionary *dictResponse = responseObject[0];

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


    NSString * myString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
Frustrated answered 30/3, 2018 at 5:59 Comment(1)
Any answer that gets the asker going in the right direction is helpful, but do try to mention any limitations, assumptions or simplifications in your answer. Brevity is acceptable, but fuller explanations are better.Battement
R
0

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

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.

Roughhouse answered 21/11, 2014 at 5:12 Comment(0)
G
0

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

Gum answered 21/11, 2014 at 5:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.