แปลง JSON string เป็น class ที่พร้อมใช้กับ C# ด้วย Visual Studio

วันก่อนเขียนบทความเรื่อง แปลง JSON STRING เป็น CLASS ที่พร้อมใช้กับ C# ไป แล้วก็มีกัลยาณมิตรได้เข้ามาแนะนำแจ้งว่า วิธีแปลง JSON string เป็น class นี่ สามารถทำได้ง่าย ๆ บน Visual Studio ได้เลยนะ ผมเองก็ถึงกับนั่งอึ้งว่ามันง่ายมากเลย แถมทียังแปลง XML เป็น class ได้ด้วย และแปลงทั้ง JSON, XML เป็น class สำหรับ VB.Net ได้อีกด้วยนะ

Feature ที่ว่านั้นชื่อเสียงเรียงนามว่าอะไร

มันมีชื่อว่า Paste Special ที่ซ่อนตัวอยู่ในเมนูบาร์ภายใต้เมนู Edit อีกที หน้าตาก็ตามภาพเลยครับ

มาลองทำกันดูเลย

ขี้เกียจพิมพ์อะไรเยอะละ เอาเป็นว่าไปลองทำตามกันได้ตามขั้นตอนต่อไปนี้เลยครับ

1. เตรียม JSON string ต้นฉบับ ให้ลองไปเอาตัวอย่างได้ที่ http://jsonapi.org/

{
  "links": {
    "self": "http://example.com/articles",
    "next": "http://example.com/articles?page[offset]=2",
    "last": "http://example.com/articles?page[offset]=10"
  },
  "data": [
    {
      "type": "articles",
      "id": "1",
      "attributes": {
        "title": "JSON API paints my bikeshed!"
      },
      "relationships": {
        "author": {
          "links": {
            "self": "http://example.com/articles/1/relationships/author",
            "related": "http://example.com/articles/1/author"
          },
          "data": {
            "type": "people",
            "id": "9"
          }
        },
        "comments": {
          "links": {
            "self": "http://example.com/articles/1/relationships/comments",
            "related": "http://example.com/articles/1/comments"
          },
          "data": [
            {
              "type": "comments",
              "id": "5"
            },
            {
              "type": "comments",
              "id": "12"
            }
          ]
        }
      },
      "links": {
        "self": "http://example.com/articles/1"
      }
    }
  ],
  "included": [
    {
      "type": "people",
      "id": "9",
      "attributes": {
        "first-name": "Dan",
        "last-name": "Gebhardt",
        "twitter": "dgeb"
      },
      "links": {
        "self": "http://example.com/people/9"
      }
    },
    {
      "type": "comments",
      "id": "5",
      "attributes": {
        "body": "First!"
      },
      "relationships": {
        "author": {
          "data": {
            "type": "people",
            "id": "2"
          }
        }
      },
      "links": {
        "self": "http://example.com/comments/5"
      }
    },
    {
      "type": "comments",
      "id": "12",
      "attributes": {
        "body": "I like XML better"
      },
      "relationships": {
        "author": {
          "data": {
            "type": "people",
            "id": "9"
          }
        }
      },
      "links": {
        "self": "http://example.com/comments/12"
      }
    }
  ]
}

2. เปิด Visual Studio ขึ้นมา ถ้ายังไม่มีก็โหลดและติดตั้งให้เสร็จเองนะครับ (ฮา)

3. เลือกเมนู File -> New -> File แล้วเลือก C# Class และกด Open

4. ลบ Initial code ออกจากไฟล์ให้หมด

5. เลือกเมนู Edit-> Paste Special -> Paste JSON As Classes

6. ผลลัพธ์ได้เป็น C# class พร้อมใช้แล้ว

public class Rootobject
{
    public Links links { get; set; }
    public Datum[] data { get; set; }
    public Included[] included { get; set; }
}

public class Links
{
    public string self { get; set; }
    public string next { get; set; }
    public string last { get; set; }
}

public class Datum
{
    public string type { get; set; }
    public string id { get; set; }
    public Attributes attributes { get; set; }
    public Relationships relationships { get; set; }
    public Links3 links { get; set; }
}

public class Attributes
{
    public string title { get; set; }
}

public class Relationships
{
    public Author author { get; set; }
    public Comments comments { get; set; }
}

public class Author
{
    public Links1 links { get; set; }
    public Data data { get; set; }
}

public class Links1
{
    public string self { get; set; }
    public string related { get; set; }
}

public class Data
{
    public string type { get; set; }
    public string id { get; set; }
}

public class Comments
{
    public Links2 links { get; set; }
    public Datum1[] data { get; set; }
}

public class Links2
{
    public string self { get; set; }
    public string related { get; set; }
}

public class Datum1
{
    public string type { get; set; }
    public string id { get; set; }
}

public class Links3
{
    public string self { get; set; }
}

public class Included
{
    public string type { get; set; }
    public string id { get; set; }
    public Attributes1 attributes { get; set; }
    public Links4 links { get; set; }
    public Relationships1 relationships { get; set; }
}

public class Attributes1
{
    public string firstname { get; set; }
    public string lastname { get; set; }
    public string twitter { get; set; }
    public string body { get; set; }
}

public class Links4
{
    public string self { get; set; }
}

public class Relationships1
{
    public Author1 author { get; set; }
}

public class Author1
{
    public Data1 data { get; set; }
}

public class Data1
{
    public string type { get; set; }
    public string id { get; set; }
}

 

เป็นไงหล่ะ ง่ายสุด ๆ เลย ไม่ต้องเมื่อยนั่งพิมพ์เองทั้งหมดประหยัดเวลา จะได้เอาเวลานั่งค้น stackoverflow ได้เยอะขึ้น 😛 หวังว่าจะเป็นประโยชน์นะครับ