Json序列化與還原序列化

來源:互聯網
上載者:User

標籤:自訂   txt   data   .text   步驟   lin   turn   using   命名   

參考:http://www.cnblogs.com/caofangsheng/p/5687994.html#commentform

下載連結:http://download.csdn.net/detail/u010312811/9682971

如下:

 

Json可以很好的用於資料結構,將資料序列化或還原序列化,進而方便資料的傳遞(暫時只用過這麼點功能。。。)

具體步驟如下:

(1)添加“Json”引用,步驟完成之後,“引用”中出現“Newtonsoft.Json”

 

(2)添加命名空間

1    using Newtonsoft.Json;2    using Newtonsoft.Json.Linq;

(3)自訂類“StudentInfo”

 1    public string Name 2    { 3        get { return _name; } 4        set { _name = value; } 5    } 6    public int Age 7    { 8        get { return _age; } 9        set { _age = value; }10    }11    public bool Sex12    {13        get { return _sex; }14        set { _sex = value; }15    }

(4)序列化:

1   StudentInfo info = new StudentInfo();2   info.Name = txtName.Text;3   info.Age = int.Parse(txtAge.Text);4   info.Sex = comboBox1.Text == "男" ? true : false;5             6   string jsonData = JsonConvert.SerializeObject(info) + "\r\n";7   txtContent.Text += jsonData;

(5)還原序列化

1   string json = txtContent.Text;2   StudentInfo info = JsonConvert.DeserializeObject<StudentInfo>(json);3   txtName.Text = info.Name;4   txtAge.Text = info.Age.ToString();5   comboBox1.Text = info.Sex ? "男" : "女";

 

Json序列化與還原序列化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.