How can I read JSON from a file stored locally?

前端 未结 2 1109
梦如初夏
梦如初夏 2021-02-08 05:38

I am attempting to use JSON.Net to load in a JSON file stored locally on an ASP.Net MVC 4 site, but am having trouble pointing to the file. Here is what I am trying to do:

相关标签:
2条回答
  • 2021-02-08 06:13

    You are passing in the path and filename as your JSON payload. You need to open the file (eg. FileStream) and read the contents into a variable (eg. StreamReader) and pass the file contents as the payload to the deserializer.

    0 讨论(0)
  • 2021-02-08 06:20

    You need to read in the JSON first using a FileStream.

    Try this.

    using(StreamReader sr = new StreamReader(Server.MapPath("~/Content/treatments.json")))
    {
          treatments = JsonConvert.DeserializeObject<List<Treatment>>(sr.ReadToEnd());
    }
    
    0 讨论(0)
提交回复
热议问题