2

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?)

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.