参考:http://www.cnblogs.com/caofangsheng/p/5687994.html#commentform
下载链接:http://download.csdn.net/detail/u010312811/9682971
效果图如下:
Json可以很好的用于数据结构,将数据序列化或反序列化,进而方便数据的传递(暂时只用过这么点功能。。。)
具体步骤如下:
(1)添加“Json”引用,步骤完成之后,“引用”中出现“Newtonsoft.Json”
(2)添加命名空间
using Newtonsoft.Json; using Newtonsoft.Json.Linq;
(3)自定义类“StudentInfo”
public string Name { get { return _name; } set { _name = value; } } public int Age { get { return _age; } set { _age = value; } } public bool Sex { get { return _sex; } set { _sex = value; } }
(4)序列化:
StudentInfo info = new StudentInfo(); info.Name = txtName.Text; info.Age = int.Parse(txtAge.Text); info.Sex = comboBox1.Text == "男" ? true : false; string jsonData = JsonConvert.SerializeObject(info) + "\r\n"; txtContent.Text += jsonData;
(5)反序列化
string json = txtContent.Text; StudentInfo info = JsonConvert.DeserializeObject<StudentInfo>(json); txtName.Text = info.Name; txtAge.Text = info.Age.ToString(); comboBox1.Text = info.Sex ? "男" : "女";