Home > database >  How do I place a JSON file in a project?
How do I place a JSON file in a project?

Time:12-02

My ASP.NET Core program requires some data from a REST server, but during development, I do not need real data so I want to just use a JSON file as data like:

program.cs

  DataClass data = JsonSerializer.Deserialize<DataClass>(File.ReadAllText(<path_to_json_file>));

mockData.json

{
  "foo": "bar",
  ...
}

Where do I place the mockData.json and how do I write the path to the JSON file in program.cs?

CodePudding user response:

You can place it anywhere in your project. Example:

enter image description here

In this case the path will be: <FileName>. So in code:

using (var sr = new StreamWriter("MyFileName.json")){ ... }

But for a json file to be readible you will need to change the properties. Right click on the file and ensure it is being loaded as content and being re-loaded when there is a newer file.

enter image description here

And that's it. No special instructions.

  • Related