每秒从jsonweb服务获取数据

本文关键字:获取 数据 服务 jsonweb | 更新日期: 2023-09-27 18:28:49

我为一个windowsphone项目创建了一个json web服务。我需要每秒钟从web服务中恢复数据,我使用带有线程的DispatcherTimer系统

private void client_DownloadInfoConf(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            //some code
            DispatcherTimer TradeThread = new DispatcherTimer();
            TradeThread.Interval = TimeSpan.FromMilliseconds(1000);
            TradeThread.Tick += new EventHandler(dispatcherTimer_Tick);
            TradeThread.Start();
        }
        else
            MessageBox.Show("Error : " + e.Error);
    }
    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        WebClient client_quest = new WebClient();

        client_quest.DownloadStringAsync(new Uri("url" + info_conf.id));
        client_quest.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadQuestConf); 
    }
    private void client_DownloadQuestConf(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            string text = e.Result;
            QuestConf resultat = JsonConvert.DeserializeObject<QuestConf>(e.Result);
            string sub = texte.Substring(0, 5);
            TextBlock text_quest = new TextBlock();
            foreach (var cust in resultat)
            {
                //The problem is here, i can't retrieve data from the object
            }
        }
        else
            MessageBox.Show("Error" + e.Error);
    }

这里的问题是,如果我试图从"var cust"中获取数据,就会发生错误,我无法从对象中提取数据。。。

类,用于提取json web服务的数据

 public class Question
    {
        public string id { get; set; }
        public string category { get; set; }
        public string name { get; set; }
        public string value { get; set; }
    }
    public class Answer
    {
        public string id { get; set; }
        public string label { get; set; }
    }
    public class QuestConf:List<object>
    {
        public Question question { get; set; }
        public List<Answer> answers { get; set; }
    }

提前感谢您的帮助

每秒从jsonweb服务获取数据

如果您从web服务获取数据并希望反序列化到对象,我认为您应该将[DataContract]和[DataMember]属性添加到类和属性中。对您从web服务中检索到的每个对象都这样尝试:

[DataContract]
public class QuestConf:List<object>
{
    [DataMember]
    public Question question { get; set; }
    [DataMember]
    public List<Answer> answers { get; set; }
}