JSON を使用してサーバーに画像を送信しようとしています。NSJSONSerialization: The function i use:
- (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/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5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mnP2fNK9HmUqqSUIVvO4I8Mcm+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\/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL\/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMi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J2r8Q0eJpx\/wjtlENX7OI+L8V4MYbPOD8g4iwOd57mNSmsXmeCyfD5QsoVGKtBwoc6xGKmnP2fNK9HmUqqSUIVvO4I8Mcm+kfX4f8QuJeEMTw3wvlsMSsJl1fOK+ZvN1VlKEuWpeM8NSozSclb35JqLu5On\/9k=",
"startdate":1377475200000}
},
"function":"addattachment"}
元の base64 のすべてのスラッシュ (/) はバックスラッシュ () でエスケープされています。したがって、データが破損しています。JSONSerialization に文字列をエスケープしないことを知らせるオプションはありますか?
よろしくお願いします。