Google Code Prettify

[C#]JSON資料轉換範例

本範例的資料轉換採用的是 JSON.NET 套件。

Source Code
using System;

using Newtonsoft.Json;

namespace ConsoleApp1
{
    public class TestUser
    {
        public string UserName { get; set; }
        public string UserPassword { get; set; }
        public string UserMail { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            TestUser u = new TestUser {
                UserName = "morse",
                UserPassword = "1234567",
                UserMail = "morsecheng@gmail.com"
            };

            string a = JsonConvert.SerializeObject(u);
            TestUser b = JsonConvert.DeserializeObject(a);
            string c = JsonConvert.SerializeObject(b);

            Console.WriteLine(a);
            Console.WriteLine("------------");
            Console.WriteLine(b);
            Console.WriteLine(b.UserName);
            Console.WriteLine(b.UserMail);
            Console.WriteLine(b.UserPassword);
            Console.WriteLine("------------");
            Console.WriteLine(c);

            Console.WriteLine("Press any key...");
            Console.ReadKey();
        }
    }
}




Result