gpt4 book ai didi

c# - Byte[] 作为 Json 中的参数

转载 作者:行者123 更新时间:2023-11-30 15:40:21 26 4
gpt4 key购买 nike

这是我的 json:

byte[] attachmentBytes = ZipToBase64();
string json = "{ \"method\": \"Bug.add_attachment\", " +
" \"params\": [ {" +
" \"ids\": " + " \"" + "25" +"\", " +
" \"data\": " + " \"" + attachmentBytes + "\", " +
" \"file_name\": " + " \"BugReport.zip\", " +
" \"Bugzilla_login\": " + " \"mymail@hotmail.com\", " +
" \"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# 中。

最佳答案

您似乎正在尝试将 byte[] 连接到字符串。那行不通的。我猜你得到的错误是关于这个串联的编译错误,对吧?

不是返回 byte[],而是返回 Convert.ToBase64String 返回的 base64 字符串。您可以将该字符串嵌入到您的 JSON 中。

当我们讨论这个主题时,您可以通过简单地调用 File.ReadAllBytes(filePath) 来大大缩短文件读取时间,它封装了所有文件流业务,因此您不必这样做。

关于c# - Byte[] 作为 Json 中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9363246/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com