1

我正在尝试显示从 RestSharp 收集的数据。例如,我有以下代码,但不知道如何显示当前的数据。

 private void readJSON()
    {
        string url = "http://www.jsonurl.com";
        var restClient = new RestClient(url);
        var request = new RestRequest(Method.GET);
        //82.147.22.3
        //What we are requesting:value
        request.AddParameter("apikey", "xxxxxtheapikeygoesherexxxxx");

        restClient.ExecuteAsync<Entry>(request, response =>
        {
            //What to do with the JSON?
        });
    }

我知道我需要将 JSON 放在 ExecuteAsync<>() 之间,但我希望能够获取数据,例如将其放入列表框中。下面是从 JSONtoSharp.com 返回给我的结果示例。代码:

    public class Change
    {
        public string direction { get; set; }
        public int amount { get; set; }
        public int actual { get; set; }
    }

    public class itementry
    {
        public int position { get; set; }
        public int prePosition { get; set; }
        public int Weeks { get; set; }
        public string ar { get; set; }
        public string ti { get; set; }
        public Change ch { get; set; }
    }

    public class RootObject
    {
        public int charDate { get; set; }
        public int retrieved { get; set; }
        public List<Entry> entries { get; set; }
    }

我确信答案很简单,我只需要帮助,因为我完全迷失在这个问题中......找不到任何好的文档来帮助我!

注意:这是针对使用 RestSharp 和 Newtonsoft 的 Windows Phone 7 上的 C#

4

1 回答 1

2
restClient.ExecuteAsync<Entry>(request, response =>
    {
        //Supply your JSON data to a callback
        Callback(response.Data);
    });

public void Callback(string jsonResponse)
{
    var responseList = JsonConvert.DeserializeObject<RootObject>(jsonResponse);
    //Assuming you have properly setup binding properties for Listbox, databind listbox here
    YourListBox.ItemsSource = responseList.entries;
}

这里 JsonConvert 来自 NewtonSoft 包

于 2012-09-21T06:31:31.793 回答