上一篇文章中我们讲解了序列号JSON数据,这篇文章我们来讲解一下反序列化JSON数据,也就是将实体类转化成JSON数据,其实也非常简单,说白了,就是将实体初始化。
一样,我们跟着前两篇的代码,在添加 一个按钮,在按钮事件里面写代码。先看效果吧。
接下俩是按钮事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using Newtonsoft.Json;namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1 () { InitializeComponent(); } private void button1_Click (object sender, EventArgs e ) { string url = "http://www.kuaidi100.com/query?type=shunfeng&postid=367847964498" ; string getJson = HttpUitls.Get(url); MessageBox.Show(getJson); } private void button2_Click (object sender, EventArgs e ) { string url = "http://www.kuaidi100.com/query?type=shunfeng&postid=367847964498" ; string getJson = HttpUitls.Get(url); Root rt = JsonConvert.DeserializeObject<Root>(getJson); MessageBox.Show("com=" + rt.com + "\r\n" + "condition=" + rt.condition + "\r\n" + "ischeck=" + rt.ischeck + "\r\n" + "state=" + rt.state + "\r\n" + "status=" + rt.status); for (int i = 0 ; i < rt.data.Count; i++) { MessageBox.Show("Data=" + rt.data[i].context + "\r\n" + rt.data[i].location + "\r\n" + rt.data[i].time + "\r\n" + rt.data[i].ftime); } } private void button3_Click (object sender, EventArgs e ) { Root rt = new Root(); rt.com="这个是我赋值的com" ; rt.condition="这个是我赋值的condition" ; rt.ischeck="这个是我赋值的ischeck" ; rt.message="这个是我赋值的message" ; rt.state="这个是我赋值的satate" ; rt.status="这个是我赋值的statcs" ; List<DataItem> data =new List<DataItem>(); DataItem dt = new DataItem(); dt.context="这个是我赋值的内容" ; dt.time="这个是我赋值的时间" ; dt.ftime="这个是我赋值的时间" ; data.Add(dt); rt.data=data; string json = JsonConvert.SerializeObject(rt); MessageBox.Show(json); } } }
转自:https://www.cnblogs.com/zoujinhua/p/10330075.html