這裏是我的JSON:字節[]作爲一個JSON
byte[] attachmentBytes = ZipToBase64();
string json = "{ \"method\": \"Bug.add_attachment\", " +
" \"params\": [ {" +
" \"ids\": " + " \"" + "25" +"\", " +
" \"data\": " + " \"" + attachmentBytes + "\", " +
" \"file_name\": " + " \"BugReport.zip\", " +
" \"Bugzilla_login\": " + " \"[email protected]\", " +
" \"Bugzilla_password\": " + " \"mypassword\", " +
" \"summary\": " + " \"blah blah\", " +
" \"content_type\": " + " \"application/octet-stream\" " +
" } ], " +
"\"id\": 1"
+ "}";
public static byte[] ZipToBase64()
{
string filePath = @"C:\Users\John\Desktop\SomeArchive.zip";
if (!string.IsNullOrEmpty(filePath))
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
byte[] filebytes = new byte[fs.Length];
fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
string encodedData = Convert.ToBase64String(filebytes, Base64FormattingOptions.InsertLineBreaks);
string encoded = encodedData;
return filebytes;
}
return null;
}
我認爲這個問題是在attachmentBytes一部分,因爲它是一個byte []。如何在json中傳遞byte []?
代碼是用C#編寫的。
您可以將每個字節轉換爲base64string,然後在接收端將其轉換回來。 – 2012-02-20 15:16:28