How to import JsonConvert in C# application?

后端 未结 9 1346
栀梦
栀梦 2020-12-03 02:33

I created a C# library project. The project has this line in one class:

JsonConvert.SerializeObject(objectList);

I\'m getting error saying

相关标签:
9条回答
  • 2020-12-03 02:44

    right click on the project and select Manage NuGet Packages.. In that select Json.NET and install

    After installation,

    use the following namespace

    using Newtonsoft.Json;
    

    then use the following to deserialize

    JsonConvert.DeserializeObject
    
    0 讨论(0)
  • 2020-12-03 02:45

    Linux

    If you're using Linux and .NET Core, see this question, you'll want to use

    dotnet add package Newtonsoft.Json
    

    And then add

    using Newtonsoft.Json;
    

    to any classes needing that.

    0 讨论(0)
  • 2020-12-03 02:53

    JsonConvert is from the namespace Newtonsoft.Json, not System.ServiceModel.Web

    Use NuGet to download the package

    "Project" -> "Manage NuGet packages" -> "Search for "newtonsoft json". -> click "install".

    0 讨论(0)
  • 2020-12-03 02:54

    Or if you're using dotnet Core,

    add to your .csproj file

      <ItemGroup>
        <PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
      </ItemGroup>
    

    And

    dotnet restore
    
    0 讨论(0)
  • 2020-12-03 02:57

    Tools -> NuGet Package Manager -> Package Manager Console

    PM> Install-Package Newtonsoft.Json
    
    0 讨论(0)
  • 2020-12-03 03:01

    Try this in C#. It works:

    var jsonObject = JsonConvert.DeserializeObject(File.ReadAllText(MyFilePath));
    

    Import below namespaces :

    For JsonConvert: using Newtonsoft.Json;

    For File : using System.IO;

    0 讨论(0)
提交回复
热议问题