简体   繁体   中英

How to import a directive in .NET 6+?

trying to add a directive for NewtonSoft.Json to my newly made C# file. It's in.Net V6 so the file is stripped down, how do I import the Json library without adding the old framework with the brackets and class definition and everything?

I tried this in the.cs file but it doesn't work:

using NewtonSoft.Json;
Console.WriteLine(JsonConvert.SerializeObject(args));

I also tried this in the.csproj file and it also doesn't work:

<ItemGroup>
    <Using Include="NewtonSoft.Json"/>
</ItemGroup>

Any help is appreciated, I've only been trying this for an evening and I'm already stumped!

  1. Open your solution explorer tab,
  2. search for the Solution, right click it,
  3. choose Manage Nuget Packages for solution,
  4. it will open the installed tab of the NuGet package manager,
  5. go to the leftmost tab Browse, search for Newtonsoft.Json and select the project you wish to install it.

You made it.

You have wrong namespace change it to Newtonsoft.Json (after installing Newtonsoft.Json nuget package)

using Newtonsoft.Json;
Console.WriteLine(JsonConvert.SerializeObject(args));

Or

<ItemGroup>
    <Using Include="Newtonsoft.Json"/>
</ItemGroup>

PS

Probably you can consider switching to System.Text.Json which is a modern framework-provided json handling library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM