พอดีจะต้องประมวณผลกับ JSON บนโปรเจกต์ .Net – C# แล้วทีนี้ JSON ที่ต้องเอามาใช้นี่มีหลายชั้นมาก แค่มองผ่าน ๆ ก็ต้องปาดเหงื่อกันเลยทีเดียว แล้วจะมานั่งสร้าง class model ไว้รองรับทั้งหมด นอกจากจะเหนื่อยและลายตาแล้ว โอกาสผิดก็สูงด้วย แต่โชคดีที่โลกใบนี้มีคนใจดีทำตัวช่วยออกมาให้เรื่องนี้สบาย
พระเอกขี่ม้าขาว
พระเอกผู้นี้มานามว่า Json2CSharp หลักการทำงานมันง่ายมาก แค่เอา JSON String ทั้งหมดที่ valid ไปวาง เสร็จแล้วก็รอเอา Class model ที่ generate ออกมาในภาษา c# ให้ไปใช้งานต่อได้เลย
มาลองทำกันดูเลย
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. เปิดเว็บ http://json2csharp.com/
3. นำ JSON string ไปวางไว้ที่ช่องว่าง แล้วกด Generate
4. ผลลัพธ์ได้เป็น C# class พร้อมใช้แล้ว
public class Links { public string self { get; set; } public string next { get; set; } public string last { get; set; } } public class Attributes { public string title { get; set; } } public class Links2 { 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 Author { public Links2 links { get; set; } public Data data { get; set; } } public class Links3 { public string self { get; set; } public string related { get; set; } } public class Datum2 { public string type { get; set; } public string id { get; set; } } public class Comments { public Links3 links { get; set; } public List data { get; set; } } public class Relationships { public Author author { get; set; } public Comments comments { get; set; } } public class Links4 { public string self { 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 Links4 links { get; set; } } public class Attributes2 { public string __invalid_name__first-name { get; set; } public string __invalid_name__last-name { get; set; } public string twitter { get; set; } public string body { get; set; } } public class Links5 { public string self { get; set; } } public class Data2 { public string type { get; set; } public string id { get; set; } } public class Author2 { public Data2 data { get; set; } } public class Relationships2 { public Author2 author { get; set; } } public class Included { public string type { get; set; } public string id { get; set; } public Attributes2 attributes { get; set; } public Links5 links { get; set; } public Relationships2 relationships { get; set; } } public class RootObject { public Links links { get; set; } public List data { get; set; } public List included { get; set; } }
โอเคครับได้วิธีง่าย ๆ ให้เอาไปใช้งานได้แล้ว ประหยัดเวลาไปได้มากโขเลยหล่ะครับ 🙂 หวังว่าจะเป็นประโยชน์นะครับ