如何在 C# 应用程序中导入 JsonConvert?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18784697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import JsonConvert in C# application?
提问by Jainendra
I created a C# library project. The project has this line in one class:
我创建了一个 C# 库项目。该项目在一个班级中有这一行:
JsonConvert.SerializeObject(objectList);
I'm getting error saying
我收到错误说
the name JsonConvert doesn't exist in the current context.
当前上下文中不存在名称 JsonConvert。
To fix that, I added System.ServiceModel.Web.dll
to references but had no luck. How can I solve this error?
为了解决这个问题,我添加System.ServiceModel.Web.dll
了引用但没有运气。我该如何解决这个错误?
采纳答案by Sam Leach
JsonConvert
is from the namespace Newtonsoft.Json
, not System.ServiceModel.Web
JsonConvert
来自命名空间Newtonsoft.Json
,而不是System.ServiceModel.Web
Use NuGet
to download the package
使用NuGet
下载package
"Project" -> "Manage NuGet packages" -> "Search for "newtonsoft json". -> click "install".
“项目”->“管理 NuGet 包”->“搜索“newtonsoft json”。-> 单击“安装”。
回答by Krishna Deepak
right click on the project and select Manage NuGet Packages..
In that select Json.NET
and install
右键单击项目并选择Manage NuGet Packages..
在该选择Json.NET
并安装
After installation,
安装后,
use the following namespace
使用以下命名空间
using Newtonsoft.Json;
then use the following to deserialize
然后使用以下内容反序列化
JsonConvert.DeserializeObject
回答by Zanon
回答by andrew lorien
Or if you're using dotnet Core,
或者,如果您使用的是 dotnet Core,
add to your .csproj file
添加到您的 .csproj 文件
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
</ItemGroup>
And
和
dotnet restore
回答by Aminur Rahman
Tools -> NuGet Package Manager -> Package Manager Console
工具 -> NuGet 包管理器 -> 包管理器控制台
PM> Install-Package Newtonsoft.Json
回答by Evan Carroll
Linux
Linux
If you're using Linux and .NET Core, see this question, you'll want to use
如果您使用的是 Linux 和 .NET Core,请参阅此问题,您会想要使用
dotnet add package Newtonsoft.Json
And then add
然后添加
using Newtonsoft.Json;
to any classes needing that.
到任何需要它的班级。
回答by Mauricio Gracia Gutierrez
If you are developing a .Net Core WebApi or WebSite you dont not need to install newtownsoft.jsonto perform json serialization/deserealization
如果您正在开发 .Net Core WebApi 或网站,则不需要安装 newtownsoft.json来执行 json 序列化/反序列化
Just make sure that your controller method returns a JsonResult
and call return Json(<objectoToSerialize>);
like this example
只需确保您的控制器方法返回 aJsonResult
并return Json(<objectoToSerialize>);
像此示例一样调用
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
如果您正在开发.Net Framework WebApi 或网站,则需要使用 NuGet 下载并安装该newtonsoft json
包
"Project" -> "Manage NuGet packages" -> "Search for "newtonsoft json". -> click "install".
“项目”->“管理 NuGet 包”->“搜索“newtonsoft json”。-> 单击“安装”。
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://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-2.1
可以在此处找到更多详细信息 - https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-2.1
回答by shiv roy
After instaling the package you need to add the newtonsoft.json.dll into assemble path by runing the flowing command.
安装包后,您需要通过运行 flow 命令将 newtonsoft.json.dll 添加到 assemble 路径中。
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
在我们可以使用我们的程序集之前,我们必须将它添加到全局程序集缓存 (GAC)。再次打开 Visual Studio 2008 命令提示符(对于 Vista/Windows7/等。以管理员身份打开它)。并执行以下命令。gacutil /id:\myMethodsForSSIS\myMethodsForSSIS\bin\Release\myMethodsForSSIS.dll
flow this link for more informATION http://microsoft-ssis.blogspot.com/2011/05/referencing-custom-assembly-inside.html
流此链接以获取更多信息 http://microsoft-ssis.blogspot.com/2011/05/referencing-custom-assembly-inside.html
回答by Satish Babu
Try this in C#. It works:
在 C# 中试试这个。有用:
var jsonObject = JsonConvert.DeserializeObject(File.ReadAllText(MyFilePath));
Import below namespaces :
导入以下命名空间:
For JsonConvert: using Newtonsoft.Json;
对于 JsonConvert: using Newtonsoft.Json;
For File : using System.IO;
对于文件: using System.IO;