用NSJSONSerialization编码的Base64字符串是反斜杠转义的

时间:2013-08-26 09:56:32

标签: objective-c json base64 nsjsonserialization

我正在尝试使用JSON将图像发送到服务器。 数据仍然正确,直到我设置我的字典以使用NSJSONSerialization进行编码: 我使用的功能:

- (NSData*) getHTTPBody
{
    if ([NSJSONSerialization isValidJSONObject:jsonMessage])
    {
        NSError * error = Nil;
        NSData * jsonData = [NSJSONSerialization dataWithJSONObject:jsonMessage options:0 error:&error];
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        return jsonData;
    }
    return Nil;
}

使用以下输入:

Printing description of self->jsonMessage:
{
    function = addattachment;
    variables =     {
        attachment =         {
            enddate = 1377475200000;
            file = "/9j/4AAQSkZJRgABAQAAAQABAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAEKADAAQAAAABAAAAEAAAAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAAQABADAREAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFB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gfih8d7v4tXWo3em3MGtGX4nM1x4W8AWh0/IttJ2r8Q0eJpx/wjtlENX7OI+L8V4MYbPOD8g4iwOd57mNSmsXmeCyfD5QsoVGKtBwoc6xGKmnP2fNK9HmUqqSUIVvO4I8Mcm+kfX4f8QuJeEMTw3wvlsMSsJl1fOK+ZvN1VlKEuWpeM8NSozSclb35JqLu5On/9k=";
            filename = "photo.jpg";
            label = "Bhhhb ";
            startdate = 1377475200000;
        };
    };
}

这似乎是正确的。事实上,我已经测试了Base64字符串并将其转换回在线解码器,它完美无瑕。

无论如何,生成了JSON NSData对象。从测试目的来看,我将它转换回NSString。这是结果:

Printing description of jsonString:
{"variables":
{"attachment":
{"enddate":1377475200000,
"filename":"photo.jpg",
"label":"Bhhhb ",
"file":"\/9j\/4AAQSkZJRgABAQAAAQABAAD\/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAEKADAAQAAAABAAAAEAAAAAD\/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH\/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH\/wAARCAAQABADAREAAhEBAxEB\/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL\/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6\/8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ydXlv2fv2ZdF\/bz1v4bftB\/FX4T+Pvht8H\/AAXDqs\/hXwN8Qf2gfih8d7v4tXWo3em3MGtGX4nM1x4W8AWh0\/IttJ2r8Q0eJpx\/wjtlENX7OI+L8V4MYbPOD8g4iwOd57mNSmsXmeCyfD5QsoVGKtBwoc6xGKmnP2fNK9HmUqqSUIVvO4I8Mcm+kfX4f8QuJeEMTw3wvlsMSsJl1fOK+ZvN1VlKEuWpeM8NSozSclb35JqLu5On\/9k=",
"startdate":1377475200000}
},
"function":"addattachment"}

原始base64中的所有斜杠(/)都是反斜杠()转义。因此数据已损坏。 有没有让JSONSerialization知道不能逃脱字符串的选项?

非常感谢提前。

1 个答案:

答案 0 :(得分:1)

这似乎是一个后端问题。

发送到服务器:

/9j/4AAQSkZJRgABAQAAAQABAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAEKADAAQAAAABAAAAEAAAAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAAQABADAREAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBA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1fOK+ZvN1VlKEuWpeM8NSozSclb35JqLu5On/9k=

在搜索了两个字符串之后,似乎Base64字符串中有一些变化无法在客户端解析...客户端期望:输出==输入。