JSON 또는 XML을 클래스로 붙여넣기

Visual Studio에서 JSON 또는 XML 파일의 텍스트를 복사한 다음 C# 또는 Visual Basic 코드에서 클래스로 텍스트를 붙여넣을 수 있습니다. 이렇게 하려면 [붙여넣기 편집]을 선택하고 >JSON을 클래스로 붙여넣거나 XML을 클래스로 붙여넣습니다.

Screenshot of the Paste Special option from the Edit menu in Visual Studio.

편집 메뉴에 선택하여 붙여넣기 옵션이 표시되지 않으면 ASP.NET 및 웹 개발, Azure 개발 또는 .NET 데스크톱 개발과 같은 워크로드 중 하나 이상이 설치되어 있는지 확인합니다. 그런 다음 앱에 대한 프로그램 파일을 선택해야 합니다. 예를 들어 C# 앱의 경우 솔루션 탐색기 Program.cs 파일을 선택합니다.

JSON (JavaScript Object Notation) 및 XML (eXtensible Markup Language)은 데이터를 저장하고 전송하는 데 모두 사용된다는 점에서 유사합니다. 그러나 JSON은 자세한 정보가 적고 배열을 사용할 수 있습니다.

예제

Visual Studio에서 JSON을 클래스 로 붙여넣기 명령 또는 XML을 클래스 로 붙여넣기 명령을 사용하기 전에 텍스트의 자리 표시자를 만듭니다. C# 앱의 경우 다음 스크린샷과 같이 빈 네임스페이스 선언을 사용하여 이 작업을 수행할 수 있습니다.

Screenshot of an empty namespace declaration in Visual Studio that's used as a placeholder to paste in JSON or XML text.

그런 다음 중괄호 안에 JSON 또는 XML 텍스트를 붙여넣습니다.

JSON

JSON 텍스트의 예는 다음과 같습니다.

{
  "Colors": [
 {
   "numberKey": 1,
   "isPrimary": true,
   "listColors": ["Red", "Blue", "Yellow"]
 },

 {
   "numberKey": 2,
   "isPrimary": false,
   "listColors": ["Purple", "Green", "Orange"]
 } ]
}

다음은 Visual Studio에서 JSON 텍스트를 클래스로 변환하는 방법을 보여 주는 스크린샷입니다.

Screenshot of the JSON example text converted to classes by using the Paste Special feature in Visual Studio.

XML

다음은 XML 텍스트의 예입니다.

<root>
 <color>
  <id>01</id>
  <name>red</name>
  <type>primary</type>
 </color>
 <color>
  <id>02</id>
  <name>green</name>
  <type>secondary</type>
 </color>
</root>

다음은 Visual Studio에서 XML 텍스트를 클래스로 변환하는 방법을 보여 주는 코드 예제입니다.

using System;

namespace PasteDemo
{
    // NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class root
    {

        private rootColor[] colorField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("color")]
        public rootColor[] color
        {
            get
            {
                return this.colorField;
            }
            set
            {
                this.colorField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class rootColor
    {

        private byte idField;

        private string nameField;

        private string typeField;

        /// <remarks/>
        public byte id
        {
            get
            {
                return this.idField;
            }
            set
            {
                this.idField = value;
            }
        }

        /// <remarks/>
        public string name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        /// <remarks/>
        public string type
        {
            get
            {
                return this.typeField;
            }
            set
            {
                this.typeField = value;
            }
        }
    }
}

참고 항목