異世界


2018年8月18日 星期六

JSON 資料轉換

image

using System.Windows.Forms;


namespace jgeNHttpTest
{

public partial class Form1 : Form
     {

/* [Jeremy] 測試<> :  JSON 資料轉換 (Class => JSON) */
private void RequestJsonTest(object s, HttpRequestEventArgs e)
{
     List<jUser> users = new List<jUser>();
     jUser jOne = new jUser();
     jOne.Name = "FirstOne";
     users.Add(new jUser());
     users.Add(new jUser());
    users.Add(new jUser());
     users.Add(new jUser());
     string json = JsonConvert.SerializeObject(jOne);
     string jUsers = JsonConvert.SerializeObject(users);

    using (var writer = new StreamWriter(e.Response.OutputStream))
     {
         writer.Write(string.Format("<p>string json =

                   JsonConvert.SerializeObject(jOne) : {0}</p>", json));
         writer.Write(string.Format ("<p>================</p>"));
         writer.Write(string.Format("<p>string jUsers =     

               JsonConvert.SerializeObject(users) : {0}</p>", jUsers));
      }

}


/* [Jeremy] 測試<> :  JSON 資料轉換 (Class => JSON)
private void RequestJsonTest(object s, HttpRequestEventArgs e)
  {
             List<jUser> users = new List<jUser>();
             jUser jOne = new jUser();
             jOne.Name = "FirstOne";
             users.Add(new jUser());
             users.Add(new jUser());
             users.Add(new jUser());
             users.Add(new jUser());
             string json = JsonConvert.SerializeObject(jOne);
             string jUsers = JsonConvert.SerializeObject(users);

            using (var writer = new StreamWriter(e.Response.OutputStream))
             {
                 writer.Write(string.Format("<p>string json = JsonConvert.SerializeObject(jOne) : {0}</p>", json));
                 writer.Write(string.Format("<p>=============================</p>"));
                 writer.Write(string.Format("<p>string jUsers = JsonConvert.SerializeObject(users) : {0}</p>", jUsers));
             }
         }


/* for JSON Data Test */
/* for JSON.NET => Newtonsoft.Json */
public class jUser
{
     public jUser()
     {
         Name = "jason";
         ID = "A1234567890";
         Age = 18;
         Address = "";
         Phone = "(02)111-222-333";
         Counte++;
     }
     public static int Counte { get; set; } //.. static, 不會被轉成 JSON
     public string Name { get; set; }
     public string ID { get; set; }
     public int Age { get; set; }
     public string Address { get; set; }
     public string Phone { get; set; }
}

}


   


沒有留言:

張貼留言