我创建了接受POST JSON文件的Web API服务。使用该服务,我想使用JSON.NET解析JSON文件。我已经看到有关该主题的多篇文章,但是,我不想将该文件保存到磁盘。我想将文件保存在内存中,解析文件并将文件处理到内存中。在Web API中访问POST JSON文件
我也在使用.NET Framework 4.0。
编辑:我应该更清楚。当文件被张贴时,它是一个文件流。我不知道的部分是如何将流转换为JSON。
public HttpResponseMessage Post()
{
HttpResponseMessage result = null;
int FileLen;
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count > 0)
{
string MyString = string.Empty;
var postedFile = httpRequest.Files[0];
FileLen = postedFile.ContentLength;
byte[] input = new byte[FileLen];
System.IO.Stream testStream = postedFile.InputStream;
testStream.Read(input, 0, FileLen);
for (int Loop1 = 0; Loop1 < FileLen; Loop1++)
MyString = MyString + input[Loop1].ToString();
CurrentRate.JSONSerializer(MyString);
}
尝试使用[JsonTextReader](http://www.newtonsoft.com/json/help/html/ReadJsonWithJsonTextReader.htm)。它需要一个[StringReader](https://msdn.microsoft.com/en-us/library/system.io.stringreader.aspx),它可以让你保留内存中的内容(这是一个答案,并决定它是一个不好的答案,因为我没有时间提供一个例子:() –
@BobbyD请检查我的编辑 – user1647160