JSON または XML をクラスとして貼り付ける

Visual Studio では、JSON ファイルまたは XML ファイルからテキストをコピーし、そのテキストをクラスとして C# または Visual Basic コードに貼り付けることができます。 そのためには、[編集]>[形式を選択して貼り付け] を選択し、[JSON をクラスとして貼り付ける] または [XML をクラスとして貼り付ける] を選択します。

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

ヒント

[編集] メニューに [形式を選択して貼り付け] オプションが表示されない場合は、次のワークロードのうち少なくとも 1 つがインストールされていることを確認します: ASP.NET と Web 開発Azure 開発.NET デスクトップ開発。 その後、目的のアプリのプログラム ファイルを選んでいることを確認します。 たとえば、C# アプリでは、ソリューション エクスプローラーProgram.cs ファイルを選択します。

JSON (JavaScript Object Notation) と XML (eXtensible Markup Language) は、どちらもデータの格納と転送に使用される点で似ています。 ただし、JSON の方が冗長が少なく、配列を使用できます。

Visual Studio で [JSON をクラスとして貼り付ける] コマンドまたは [XML をクラスとして貼り付ける] コマンドを使う前に、テキスト用のプレースホルダーを作成します。 C# アプリの場合は、次のスクリーンショットに示すように、空の namespace 宣言を使ってこれを行うことができます。

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;
            }
        }
    }
}

関連項目