developer tip

C # 애플리케이션에서 JsonConvert를 가져 오는 방법은 무엇입니까?

copycodes 2020. 10. 4. 11:44
반응형

C # 애플리케이션에서 JsonConvert를 가져 오는 방법은 무엇입니까?


C # 라이브러리 프로젝트를 만들었습니다. 이 프로젝트는 한 클래스에 다음 줄이 있습니다.

JsonConvert.SerializeObject(objectList);

다음과 같은 오류가 발생합니다.

JsonConvert라는 이름이 현재 컨텍스트에 존재하지 않습니다.

이를 수정하기 위해 System.ServiceModel.Web.dll참조에 추가 했지만 운이 없었습니다. 이 오류를 어떻게 해결할 수 있습니까?


JsonConvert네임 스페이스에서 가져온 것이 Newtonsoft.Json아니라System.ServiceModel.Web

NuGet다운로드에 사용package

"프로젝트"-> "NuGet 패키지 관리"-> ""newtonsoft json "검색->"설치 "클릭.


프로젝트를 마우스 오른쪽 버튼으로 클릭하고 Manage NuGet Packages..In that select Json.NETand install

설치 후

다음 네임 스페이스 사용

using Newtonsoft.Json;

그런 다음 다음을 사용하여 deserialize

JsonConvert.DeserializeObject

NuGet을 사용하여 설치합니다.

Install-Package Newtonsoft.Json


답변으로 게시 합니다 .


또는 dotnet Core를 사용하는 경우

.csproj 파일에 추가

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

dotnet restore

도구-> NuGet 패키지 관리자-> 패키지 관리자 콘솔

PM> Install-Package Newtonsoft.Json

리눅스

Linux 및 .NET Core 를 사용하는 경우이 질문을 참조하세요.

dotnet add package Newtonsoft.Json

그리고 추가

using Newtonsoft.Json;

필요한 모든 수업에.


.Net Core WebApi 또는 WebSite를 개발하는 경우 json 직렬화 / 비 현실화 를 수행하기 위해 newtownsoft.json설치할 필요가 없습니다.

컨트롤러 메서드 가이 예제와 같이 a JsonResult및 호출을 반환하는지 확인하십시오.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);
        }
    }
}

.Net Framework WebApi 또는 WebSite를 개발하는 경우 NuGet을 사용하여 newtonsoft json패키지 를 다운로드하고 설치해야 합니다.

"프로젝트"-> "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


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

참고URL : https://stackoverflow.com/questions/18784697/how-to-import-jsonconvert-in-c-sharp-application

반응형