WPF(6)---json数据的接收与发送


模拟发送:{"op":"UpdateDeviceStatus","monitor":{"File":"true","Smoke":"true"}}

获取命令:{"op":"GetMessage","issuccess":null,"context":{}}

服务端返回命令:"{"op":null,"issuccess":"true","context":{"File":"true","Smoke":"true"}}

对json的解析:

                //将字符串转为json形式
                JObject MyJObject = JObject.Parse(PostData);
                //解码json
                Myop = MyJObject["op"].ToString();

1----webservce服务端

  (1)序列化JSON数据({"op":null,"issuccess":null,"context":{}})

 //序列化json数据
        private class SendJson
        {
            public string op { get; set; }
            public string issuccess { get; set; }
            private Dictionary<string,object> _context = null;
            public Dictionary<string, object> context
            {
                get
                {
                    if (this._context == null)
                    {
                        this._context = new Dictionary<string, object>();
                    }
                    return this._context;
                }
                set
                {
                    this._context = value;
                }
            }
        }
        public class _Data  //暂时保存数据
        {
            public static string _MyFile = "null";
            public static string _MySmoke = "null"; 
        } 

  (2)对请求方式的判断

 if (context.Request.RequestType != "POST" || context.Request.ContentType.ToLower().IndexOf("application/json") == -1)
            {
               SendJson MySendJson = new SendJson();
               string strMySendJson = JsonConvert.SerializeObject(MySendJson);
               context.Response.ContentType = "application/json";
               context.Response.Write(strMySendJson);
               return;
           }

(3)接收传入数据

 //当请求方式为POST时 获取传入内容
            var StreamDate = context.Request.InputStream;
            string MyFile;
            string MySmoke;
            string Myop;
            using (StreamReader MyStreamReader = new StreamReader(StreamDate))
            {   
                //读传入的数据
                string PostData = MyStreamReader.ReadToEnd();
                //对传入的URL数组进行解码 转换为字符串
                PostData = System.Web.HttpUtility.UrlDecode(PostData);
                //将字符串转为json形式
                JObject MyJObject = JObject.Parse(PostData);
                //解码json
                Myop = MyJObject["op"].ToString();
                //对op方法进行判断
                if (Myop == "UpdateDeviceStatus")
                {
                    MyFile = MyJObject["monitor"]["File"].ToString();
                    MySmoke = MyJObject["monitor"]["Smoke"].ToString();
                    _Data._MyFile = MyFile;
                    _Data._MySmoke = MySmoke;
                    context.Response.ContentType = "application/json";
                    context.Response.Write(SendMessage(Myop, MyFile, MySmoke));
                }
                if (Myop == "GetMessage")
                {
                    MyFile = _Data._MyFile;
                    MySmoke = _Data._MySmoke;
                    context.Response.ContentType = "application/json";
                    context.Response.Write(SendMessage(Myop, MyFile, MySmoke));
                }
            }
        }

(4)数据处理过程

        private string SendMessage(string Myop,string MyFile,string MySmoke)
        {
            SendJson MySendJson = new SendJson();
            switch(Myop)
            {
                case "UpdateDeviceStatus":
                    if(MyFile!=""&MySmoke!="")
                    {
                        MySendJson.issuccess="true";
                        MySendJson.op = "updateDeviceStatus";
                    }
                    else
                    {
                        MySendJson.op = "updateDeviceStatus";
                        MySendJson.issuccess="false";
                    } break;
                case "GetMessage":
                     if(MyFile=="false")
                     {
                         MySendJson.issuccess="true";
                         MySendJson.context.Add("File", "false");
                     }
                    else if(MyFile=="true")
                     {
                         MySendJson.issuccess="true";
                         MySendJson.context.Add("File", "true");
                     }
                     if(MySmoke=="false")
                     {
                         MySendJson.issuccess="true";
                         MySendJson.context.Add("Smoke", "false");
                     }
                     else if (MySmoke == "true")
                     {
                         MySendJson.issuccess="true";
                         MySendJson.context.Add("Smoke", "true");
                     }
                     if (MySmoke == "null" & MyFile == "null")
                     {
                         MySendJson.issuccess = "false";
                         MySendJson.context.Add("Smoke", "false");
                         MySendJson.context.Add("File", "false");
                     }
                    break;
                default:
                    MySendJson.op = "方法错误";
                break;
            }
            return JsonConvert.SerializeObject(MySendJson);
        }

2---客户端接收与发送json数据

   1===》发送

   (1)序列化json数据格式

 private class MoNiSendJson
        {
            public string op { get; set; }
            private Dictionary<string, object> _context = null;
            public Dictionary<string, object> monitor
            {
                get
                {
                    if (this._context == null)
                    {
                        this._context = new Dictionary<string, object>();
                    }
                    return this._context;
                }
                set
                {
                    this._context = value;
                }
            }
        }

  (2)发送json数据

 private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            //创建发送json字符串
            MoNiSendJson MyMoNiSendJson=new MoNiSendJson();
            MyMoNiSendJson.op = "UpdateDeviceStatus";
            MyMoNiSendJson.monitor.Add("File","true");
            MyMoNiSendJson.monitor.Add("Smoke","true");
            string strMySendJson = JsonConvert.SerializeObject(MyMoNiSendJson);
            //post方式发送
            HttpWebRequest MyHttpWebRequest = null;
            HttpWebResponse MyHttpWebResponse = null;
            MyHttpWebRequest = (HttpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings["MyUrl"]);
            MyHttpWebRequest.ContentType = "application/json";
            byte[] Mybyte = Encoding.UTF8.GetBytes(strMySendJson);
            MyHttpWebRequest.ContentLength = (long)Mybyte.Length;
            MyHttpWebRequest.Method = "POST";
            Stream MyStream = MyHttpWebRequest.GetRequestStream();
            MyStream.Write(Mybyte, 0, Mybyte.Length);
            MyStream.Close();
            //接收过程
            MyHttpWebResponse = (HttpWebResponse)MyHttpWebRequest.GetResponse();
            StreamReader MyStreamReader = new StreamReader(MyHttpWebResponse.GetResponseStream(), Encoding.UTF8);
            string MyJsonRead = MyStreamReader.ReadToEnd(); MyStreamReader.Close(); MyHttpWebResponse.Close();
        }

 2===》接收json

    (1)序列化json

//序列化json数据
        private class SendJson
        {
            public string op { get; set; }
            public string issuccess { get; set; }
            private Dictionary<string, object> _context = null;
            public Dictionary<string, object> context
            {
                get
                {
                    if (this._context == null)
                    {
                        this._context = new Dictionary<string, object>();
                    }
                    return this._context;
                }
                set
                {
                    this._context = value;
                }
            }
        }

  (2)接收json数据

 private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            //创建发送json字符串
            SendJson MySendJson = new SendJson();
            MySendJson.op = "GetMessage";
            string strMySendJson = JsonConvert.SerializeObject(MySendJson);
            //post方式发送
            HttpWebRequest MyHttpWebRequest = null;
            HttpWebResponse MyHttpWebResponse = null;
            MyHttpWebRequest = (HttpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings["MyUrl"]);
            //请求的数据格式
            MyHttpWebRequest.ContentType = "application/json";
            //设置请求方式
            MyHttpWebRequest.Method = "POST";
            //转换为byte[] 数组
            byte[] Mybyte = Encoding.UTF8.GetBytes(strMySendJson);
            //设置标头
            MyHttpWebRequest.ContentLength = (long)Mybyte.Length;
            //创建stream 用来发送
            Stream MyStream = MyHttpWebRequest.GetRequestStream();
            MyStream.Write(Mybyte, 0, Mybyte.Length);
            MyStream.Close();
            //接收数据
            MyHttpWebResponse = (HttpWebResponse)MyHttpWebRequest.GetResponse();
            StreamReader MyStreamReader = new StreamReader(MyHttpWebResponse.GetResponseStream(), Encoding.UTF8);
            string MyJsonRead = MyStreamReader.ReadToEnd();
            MyStreamReader.Close();
            MyHttpWebResponse.Close();
            //将接收道德数据转换为jsoon 字符形式 
            JObject jo = JObject.Parse(MyJsonRead);
            //对发来数据解析
            string Fire=jo["context"]["File"].ToString();
            string Smoke = jo["context"]["Smoke"].ToString();
            if (Fire == "true")
            {
                this.lblFire.Content = "着火了";
            }
            else
            {
                this.lblFire.Content = "没有着火!";
            }
            if (Smoke == "true")
            {
                this.lblSmoke.Content = "有烟";
            }
            else
            {
                this.lblSmoke.Content = "无烟!";
            }
        }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM