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等等以下低版本的浏览器是没有 这里要注意

原文地址:https://www.cnblogs.com/yannis/p/2608484.html