zoukankan      html  css  js  c++  java
  • silverlight实时从Javascript获取json数据

    //C#代码   
    
      public void GetJsonDate()
            {
                object o = System.Windows.Browser.HtmlPage.Window.Eval("GetJsonDate();");
                if (o != null)
                {
                    if (o.ToString() == "over")
                    {
                        LoadValue();
                        System.Windows.Threading.DispatcherTimer dti = new System.Windows.Threading.DispatcherTimer()
                             {
                                 Interval = TimeSpan.FromMilliseconds(3000)
                             };
                        dti.Tick += (s, e) =>
                        {
                            dti.Stop();
                            GetJsonDate();
                        };
                        dti.Start();
                    }
                    else
                    {
                        DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(Optical));
                        System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.Unicode.GetBytes(o.ToString()));
                        opticallist.Add((Optical)json.ReadObject(ms));
                        GetJsonDate();
                    }
                }
            }
    
    //DataContractJsonSerializer  需要引用using //System.Runtime.Serialization.Json;
    //System.Runtime.Serialization;
    //System.ServiceModel
    /*
    *思路:如果javascript传入over字符串 读取让数据并3秒继续执行,否则继续获取
    */
    
       window.onload = function () {
                //       slHost = document.getElementById("silverlightControl");
                //     page = slHost.contentDocument;
                GetDate();
            };
    
            function GetDate() {
                $.post('/Page/TeleCable/GetData.aspx', { equipid: 4 }, function (data) {
                    jsondate = data;
                });
                setTimeout(GetDate, 3000);
            }
    
            function GetJsonDate() {
                if (jsondate) {
                    if (jsonCount < jsondate.length) {
                        var j = jsondate[jsonCount];
                        jsonCount++;
                        return (JSON.stringify(j));
                    }
                    else {
                        jsonCount = 0;
                        jsondate = null;
                        return "over";
                    }
                }
            }
    

     JSON这个对象 IE8等等以下低版本的浏览器是没有 这里要注意

  • 相关阅读:
    JSTL 标签库
    C++(一)— stringstream的用法
    深度学习—反向传播的理解
    深度学习—线性分类器理解
    Python—numpy.bincount()
    Python—numpy.argsort()
    Python—numpy.flatnonzero()
    C++(零)— 提高程序运行效率
    机器学习(八)—GBDT 与 XGBOOST
    机器学习(七)—Adaboost 和 梯度提升树GBDT
  • 原文地址:https://www.cnblogs.com/yannis/p/2608484.html
Copyright © 2011-2022 走看看