asp.netjsonc#-4.0server.mappath

How to deserialize the data of a json document stored locally in the project folder


A json file is stored in the Project folder.I want to deserialize data of that json document.Im getting error while fetching the path of json document.

I used this code and got error "An exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll but was not handled in user code"

        string file = HttpContext.Current.Server.MapPath("~/project.Services/xyz/myjsonfile.json");

            using (StreamReader reader = new StreamReader(file))
            {
              collection = JsonConvert.DeserializeObject<Dictionary<string, string>>(reader.ReadToEnd());             
           }

Solution

  • below code worked for me

                using (Stream stream = this.GetType().Assembly.GetManifestResourceStream(this.GetType().Assembly.GetName().Name + "." + filename))
             {using (StreamReader reader = new StreamReader(stream))
                {
                    collection = JsonConvert.DeserializeObject<Dictionary<string, string>>(reader.ReadToEnd());
                }}
    

    Thank You Michael