-1

Question:
What is the best way to pull JSON from a .json file on my server, into code, so it can be deserialized into a .NET object?

Details:
I have a file called "backlog.json" saved in my VS2013 Express MVC project. The file was not created by my project - it has been added to the project by doing "Add Existing Item."

"backlog.json" is a collection of json objects. I need to pull these objects from the collection, so I can add to the database my project uses and manipulate with my project.

The project uses .NET Framework 4.5, and I've added NewtonSoft's Json.NET to the project, using NuGet package manager. Once I get the objects from "backlog.json" into my code, I'll use Json.NET to deserialize and manipulate the objects through C#.

For example, something like:

public void AddJSON(EFWorkOrderModelContainer WorkOrderContainer)
{
    // Pull backlog.json from C:\Project\App_Data\backlog.json
    // De-serialize and parse
}
Aaron Thomas
  • 5,054
  • 8
  • 43
  • 89
  • What do you mean by `What is the best way to pull JSON from a .json file on my server` ? Just read it. – EZI Feb 17 '15 at 18:57
  • @EZI let me try to clarify... I have a .JSON file saved in my project. I need to use it in code. What's the best way to use a server-side file in code like this? – Aaron Thomas Feb 17 '15 at 18:59
  • Have a look at this post : http://stackoverflow.com/questions/2546138/deserializing-json-data-to-c-sharp-using-json-net – Noel Feb 17 '15 at 19:00
  • Json is just a plain text. Read it from file. Seems like you have some misunderstandings but I don't know what. – EZI Feb 17 '15 at 19:01
  • @EZI I guess that's what I'm asking here - what steps are required to read from file? – Aaron Thomas Feb 17 '15 at 19:03
  • @AaronThomas no steps. `string json = File.ReadAllText(....)` – EZI Feb 17 '15 at 19:04
  • @EZI: so, what namespace is required for File.ReadAllText? I'm trying to figure out what to put in my `using` statement... – Aaron Thomas Feb 17 '15 at 19:10
  • @EZI unfortunately this is exactly where I need help. MSDN doc link is broken (ref https://msdn.microsoft.com/en-us/library/system.io.file.readalltext%28v=vs.110%29.aspx and https://msdn.microsoft.com/en-us/library/ms143368(v=vs.110).aspx). File IO is exactly what this question is about... I'll keep plugging away at it I guess... – Aaron Thomas Feb 17 '15 at 19:17
  • @Noel: post you ref'd is about deserializing - I need to bring in my .JSON file first. – Aaron Thomas Feb 17 '15 at 19:18
  • Are you asking how to load a file ? Is the file part of your solution or is i a standalone file? – Noel Feb 17 '15 at 19:20
  • @Noel: yes, that is correct. Trying to load a .JSON file. It is a standalone file that I've added to my project. `string myjson = File.ReadAllText("~\App_Data\backlog.json");` gives an error, says unrecognized escape sequence. – Aaron Thomas Feb 17 '15 at 19:24
  • If the file is part of the solution , can you set it as an embedded resource and load like so: http://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file – Noel Feb 17 '15 at 19:27
  • Ahh, actually if I do `string json = File.ReadAllText(@"C:\Project\App_Data\backlog.json");` it seems to work fine. Anyone care to post as an answer, I'll mark as accepted... unless there's a better way to go about this? – Aaron Thomas Feb 17 '15 at 19:31

2 Answers2

1

You can't do string myjson = File.ReadAllText("~/App_Data/backlog.json"); because File class doesn't know how to convert from an application root relative path. You can do string myjson = File.ReadAllText(Server.MapPath("~/App_Data/backlog.json")); or if Server isn't in scope string myjson = File.ReadAllText(HttpContext.Current.Server.MapPath("~/App_Data/backlog.json"));

You should not do string json = File.ReadAllText(@"C:\Project\App_Data\backlog.json"); because that's dependent on where you place the website within your web server, and that's a bad idea.

Yes, MSDN is down right now, so you can't use that to look up the namespace for the File class. But in Visual Studio, if you just type File you can use Intellisense to have it automatically import the namespace for you by right-clicking the File.

mason
  • 31,774
  • 10
  • 77
  • 121
  • Anyone care to explain the downvote so I know what to improve? – mason Feb 17 '15 at 19:54
  • i didn't downvote. Your solution isn't working as expected, but I'm working on narrowing down why. – Aaron Thomas Feb 17 '15 at 20:08
  • looks like I have to include forward slashes or double backslashes with `.MapPath`... but that gets it. Thank you. – Aaron Thomas Feb 17 '15 at 20:10
  • @AaronThomas I fixed the slashes in my answer. You should use forward slashes when it's a URL, or backwards slashes for a Windows based path, and you either have to escape backslashes or use a string literal (`@""`). – mason Feb 17 '15 at 20:24
0

I won't mark my own answer as "answered," but a similar way of going about this is actually described on Newtonsoft.com's site. Click here to see the sample... it describes exactly what I was looking for.

Aaron Thomas
  • 5,054
  • 8
  • 43
  • 89