1

I upload a JSON file with a HTML form as explained here in the first paragraph. I accept only 1 file at a time so this is my controller:

public IActionResult Upload(IFormFile file)
{
}

Now I want to convert the file containing JSON to an object. Just like this accepted answer of Cuong Le. How do I convert the file to lets say MyObject? How do i deserialize the file? (Newtonsoft is the lib to import right?)

Kip ei
  • 479
  • 6
  • 20

1 Answers1

5

You can read the text from the file and then convert to JSON. You can try something like,

string fileContent = null;
       using (var reader = new StreamReader(file.OpenReadStream()))
       {
         fileContent = reader.ReadToEnd();
       }
   var result = JsonConvert.DeserializeObject<MyObject>(fileContent );

Yes, you can use Newtonsoft NuGet package for deserializing.

M.S
  • 288
  • 4
  • 12