0

I have an NSDictionary (params) which contains the below data:

{
    deviceType = iPhone;
    ordersActionList = ({
            endDate = "07/14/2014 14:32";
            orderId = 2807171;
            reason = "Some reason";
        }
    );
}

When I parse the above using NSJSONSerialization:

NSError *err;
NSData *jsonData =[NSJSONSerialization dataWithJSONObject:params options:0 error:&err]; 

It adds escape characters to endDate, the endDate shows up as below:

 `"endDate": "07\/14\/2014 14:32"`

This is incorrect, can someone suggest how I can avoid escape characters in JSON when serializing using NSJSONSerialization?

EDIT: This is my work around for now but I don't like doing this, I was hoping for a better solution:

-(NSData*) converToNSData:(NSDictionary *)params
{
    NSError * err;
    NSData *jsonData =[NSJSONSerialization dataWithJSONObject:params options:0 error:&err];

    NSString *jsonStr1 = [NSString stringWithUTF8String:[jsonData bytes]];
    jsonStr1 = [jsonStr1 stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];

    NSData *jsonData2 =[jsonStr1 dataUsingEncoding:NSUTF8StringEncoding];

    return jsonData2;
}
DP2
  • 635
  • 9
  • 22
  • 2
    Which is precisely what it should do. – Hot Licks Jul 14 '14 at 23:47
  • 2
    Blackslashes must be escaped within JSON strings. – Michael Mior Jul 14 '14 at 23:53
  • troop231, I already had the workaround answer which I forgot to put in my intial post but I can not use this solution initially as I should not be touching the json ... I was looking for a way to do this through NSJSONSerialization if thats not possible then I would have to send the dates differently to JSON, thanks for your suggestion though. – DP2 Jul 15 '14 at 12:16
  • Why are you solving a problem that doesn't exist??? – Hot Licks Jul 15 '14 at 15:47
  • Hot Licks, I didn't get you. I thought there should be a way to tell the NSJSONSerialization to keep the text as is. – DP2 Jul 15 '14 at 16:08
  • @HotLicks Would just like to point out that the solidus or forward slash character `/` , that is used in the date string here, is not required to be escaped at all. There are only two required escapes, the reverse solidus, AKA backslash `\ ` and the double quote `"` The fact that JSON Serialisation escapes this is wrong, because it changes the meaning of the output. It's fine if you just wanted to deserialise it, but not if you want to use it in another application which understands JSON. See http://www.json.org/ for the spec – Matt Fellows May 18 '17 at 13:30
  • @MattFellows -- Correct -- see [json.org](http://www.json.org/). Look at the graphic for ***string***. What do you see? – Hot Licks May 18 '17 at 22:19
  • @HotLicks I see that you *can* represent a Solidus by escaping, but that has a different meaning. In the same way as `b` is a different character than `\b`, an escaped solidus is different to a solidus. `b` is not required to be escaped though if you want that character. Read the text above the picture and you'll see what is required to be escaped. – Matt Fellows May 19 '17 at 06:32
  • @MattFellows - Read the text in the graphic. `\b` is not described as "b" but rather "backspace". `\/` is described as "solidus". Any tool which accepts JSON should recognize that sequence as "solidus". So if there's another tool that is not recognizing it, that tool is broken. – Hot Licks May 19 '17 at 11:12
  • @HotLicks Unfortunately the tool in question is pretty much every web-browser, at least all the browsers I've tried. Try this in a browser console for proof. `var obj = { "str1":"this/is/not/escaped/", "str2":"this\/is\/not\/escaped" }; obj1.str1 == obj1.str2;` I'll admit that I have no idea what `\/` is meant to represent however... – Matt Fellows May 19 '17 at 11:21

3 Answers3

3

If you're talking about avoiding escaped slashes in the date, then NSJSONSerialization can't do that. However, note that the resulting JSON data is perfectly valid and if you deserialize it, you'll get the original data.

If you still want to remove the backslashes, you'll have to convert your result into an NSString and do the search/replace yourself.

Related: how to prevent NSJSONSerialization from adding extra escapes in URL

Community
  • 1
  • 1
mmh02
  • 164
  • 7
  • Thanks for link but I have been through this article already, I have a a solution, check my edit but I was hoping for better solution. Its a bit tedious to convert and replace. – DP2 Jul 15 '14 at 00:14
  • True, search/replace is pretty tedious for what you want to do, but it seems to be the best approach. – mmh02 Jul 15 '14 at 06:32
0

You can do this, however if your data has two back slashes, they will be removed. If you can guarantee your data doesn't have two back slashes, this is OK.

NSString *newString = [[yourDict objectForKey:@"endDate"] 
stringByReplacingOccurrencesOfString:@"\\" withString:@""];
klcjr89
  • 5,862
  • 10
  • 58
  • 91
0
   var returnString:String?
    do
    {
        let jsonData = try JSONSerialization.data(withJSONObject: dict, options: JSONSerialization.WritingOptions.prettyPrinted)

        if let json = String(data: jsonData, encoding: .utf8) {
            returnString=json.replacingOccurrences(of: "\\", with: "")
        }

    }
    catch {
        print("something went wrong with parsing json")
    }
Urvish Modi
  • 1,118
  • 10
  • 11