0

I have a program that takes a .cs script file as input. I want to read from a JSON file in the script and I'm trying to use the JSON.NET library to do it. However, I'm unable to properly reference the dll file to use the functions inside. I'm NOT using Visual Studio to write the script hence I cannot add the reference using the conventional method.

My code:

//css_reference Newtonsoft.Json.dll;
using Newtonsoft;
using Newtonsoft.Json;
using Newtonsoft.Json.JsonConvert;
public class Script
{
    public async void Action(String path)
    {
        StreamReader re = new StreamReader("job.json");
        JsonTextReader reader = new JsonTextReader(re);
        JsonSerializer se = new JsonSerializer();
        object parsedData = se.Deserialize(reader);

There were three files associated with Newtonsoft library, the .dll file, a .pdb file and a .Json file and all three are placed in the same folder as the script file.

Error:

Namespace Newtonsoft cannot be found in ...

Namespace Newteonsoft.Json cannot be found in ...

If I remove the three using statements, I get namespace not found for StreamReader, JsonTextReaader and JsonSerializer.

I'd like help regarding this issue. Thanks!

8
  • 1
    Did you use NuGet package manager to install Json.NET? Commented Feb 28, 2019 at 16:22
  • How is the program executing your script? It has to add references to any assemblies you intend to use. Commented Feb 28, 2019 at 16:23
  • Have you tried fully qualifying the class names in lieu of the using statements? Commented Feb 28, 2019 at 16:24
  • Try typing install-package newtonsoft.json in the package manager console and hit enter Commented Feb 28, 2019 at 16:24
  • @OlegI I have only downloaded and placed the .dll files in the same folder as the script. As I understood, NuGet is used when I use Visual studio or did I understand it wrong? Commented Feb 28, 2019 at 16:25

3 Answers 3

1

For me, solution was to use css_nuget at the top of the file, like this:

//css_nuget Newtonsoft.Json;
using Newtonsoft;
using Newtonsoft.Json;
Sign up to request clarification or add additional context in comments.

Comments

0

From what you have posted it looks like you have manually installed JSON.NET, i would recommend installing it via NuGet, that way your project should have the references to the dll's set during the installation process. I have used JSON.NET a number of time in .Net Framework and .Net Core projects and that is how i have always installed it and never had an issue.

1 Comment

I'm not using Visual Studio. It's a simple c# file hence I cannot add the reference to the project .
0

From within your script, you need to load the assembly for JSON.net, using System.Reflection.Assembly.Load(). You should then be able to use reflection to create instances of the JSON.net classes and invoke methods on them. If you combine this with the dynamic runtime, you should be able to instantiate objects and work with them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.