2
JSON을 사용하여 서버로 이미지를 보내려고합니다. 기능은 내가 사용 :NSJSONSerialization으로 인코딩 된 Base64 문자열은 백 슬래시가 이스케이프 처리됩니다.
- (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/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQ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jtlENX7OI+L8V4MYbPOD8g4iwOd57mNSmsXmeCyfD5QsoVGKtBwoc6xGKmnP2fNK9HmUqqSUIVvO4I8Mcm+kfX4f8QuJeEMTw3wvlsMSsJl1fOK+ZvN1VlKEuWpeM8NSozSclb35JqLu5On/9k=";
filename = "photo.jpg";
label = "Bhhhb ";
startdate = 1377475200000;
};
};
}
올바른 것 같다 난 내 사전을 설정하는 곳이 NSJSONSerialization로 인코딩 할 때까지 데이터는 여전히 정확 . 사실, 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\/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NX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FmWBpZg8BUyeWHqRVFxxMoyr1adWronG3JJqM5OydXlv2fv2ZdF\/bz1v4bftB\/FX4T+Pvht8H\/AAXDqs\/hXwN8Qf2gfih8d7v4tXWo3em3MGtGX4nM1x4W8AWh0\/IttJ2r8Q0eJpx\/wjtlENX7OI+L8V4MYbPOD8g4iwOd57mNSmsXmeCyfD5QsoVGKtBwoc6xGKmnP2fNK9HmUqqSUIVvO4I8Mcm+kfX4f8QuJeEMTw3wvlsMSsJl1fOK+ZvN1VlKEuWpeM8NSozSclb35JqLu5On\/9k=",
"startdate":1377475200000}
},
"function":"addattachment"}
원래 base64의 모든 슬래시 (/)는 백 슬래시()로 이스케이프됩니다. 따라서 데이터가 손상되었습니다. 문자열을 이스케이프 처리하지 않도록 JSONSerialization에 알리는 옵션이 있습니까?
미리 감사드립니다.
디버거가 문자열 데이터를 표시하는 방식 때문에 문자 만 이스케이프 처리됩니다. 기본적으로 C 문자열 리터럴로 표시되는 것처럼 데이터가 표시됩니다. 백 슬래시는 실제 데이터에 존재하지 않습니다. –
당신 말이 맞아요. 디버깅을 더 진행했는데 서버에서 유효한 JSON을 다시 가져 왔지만 이미지가 손상되었습니다. 이유는 모르겠습니다. 그것은 서버 문제 (나는 내가 보낸 것처럼 동일한 Base64를 서버에서 기대하고 있습니다 ...하지만 그렇지 않습니다) – Reneger