How to import JsonConvert in C# application?
Asked Answered
T

9

132

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

JsonConvert.SerializeObject(objectList);

I'm getting error saying

the name JsonConvert doesn't exist in the current context.

To fix that, I added System.ServiceModel.Web.dll to references but had no luck. How can I solve this error?

Teniafuge answered 13/9, 2013 at 10:59 Comment(0)
S
210

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".

Surpass answered 13/9, 2013 at 10:59 Comment(2)
is you are building a .NET Core WebApi or WebSite see my answer belowPicardi
also, make sure 'using Newtonsoft.Json;' is thereArratoon
X
54

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
Xiomaraxiong answered 26/8, 2014 at 6:44 Comment(1)
For .net core you dont need to install newtonsoft - see my answerbelowPicardi
K
19

Install it using NuGet:

Install-Package Newtonsoft.Json


Posting this as an answer.

Kong answered 11/2, 2016 at 17:14 Comment(1)
For .net core you dont need to install newtonsoft - see my answerbelowPicardi
A
15

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.

Aiken answered 29/12, 2017 at 7:55 Comment(2)
For .net core you dont need to install newtonsoft - see my answerbelowPicardi
(This also works on Windows with .NET Core and dotnet CLI)Bot
K
8

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
Knorring answered 31/8, 2017 at 7:38 Comment(1)
For .net core you dont need to install newtonsoft - see my answerbelowPicardi
P
4

If you are developing a .Net Core WebApi or WebSite you dont not need to install newtownsoft.json to perform json serialization/deserealization

Just make sure that your controller method returns a JsonResult and call return Json(<objectoToSerialize>); like this example

namespace WebApi.Controllers
{
    [Produces("application/json")]
    [Route("api/Accounts")]
    public class AccountsController : Controller
    {
        // GET: api/Transaction
        [HttpGet]
        public JsonResult Get()
        {
            List<Account> lstAccounts;

            lstAccounts = AccountsFacade.GetAll();

            return Json(lstAccounts);
        }
    }
}

If you are developing a .Net Framework WebApi or WebSite you need to use NuGet to download and install the newtonsoft json package

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

namespace WebApi.Controllers
{
    [Produces("application/json")]
    [Route("api/Accounts")]
    public class AccountsController : Controller
    {
        // GET: api/Transaction
        [HttpGet]
        public JsonResult Get()
        {
            List<Account> lstAccounts;

            lstAccounts = AccountsFacade.GetAll();

            //This line is different !! 
            return new JsonConvert.SerializeObject(lstAccounts);
        }
    }
}

More details can be found here - https://learn.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-2.1

Picardi answered 12/6, 2018 at 10:51 Comment(0)
M
3

Tools -> NuGet Package Manager -> Package Manager Console

PM> Install-Package Newtonsoft.Json
Membrane answered 23/10, 2017 at 17:34 Comment(1)
For .net core you dont need to install newtonsoft - see my answerbelowPicardi
C
1

Please give the below command:

dotnet add package Newtonsoft.Json

and then it works.

Cetacean answered 29/5, 2023 at 10:33 Comment(0)
N
0

After instaling the package you need to add the newtonsoft.json.dll into assemble path by runing the flowing command.

Before we can use our assembly, we have to add it to the global assembly cache (GAC). Open the Visual Studio 2008 Command Prompt again (for Vista/Windows7/etc. open it as Administrator). And execute the following command. gacutil /i d:\myMethodsForSSIS\myMethodsForSSIS\bin\Release\myMethodsForSSIS.dll

flow this link for more informATION http://microsoft-ssis.blogspot.com/2011/05/referencing-custom-assembly-inside.html

Nunn answered 3/1, 2019 at 11:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.