使用RestSharp显示JSON数据

时间:2012-09-20 22:34:00

标签: c# json windows-phone-7

我正在尝试显示从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?
        });
    }

我知道我需要在ExecuteAsync&lt;&gt;()之间放置JSON,但我希望能够获取数据,例如将其放入列表框中。以下是从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#

1 个答案:

答案 0 :(得分: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包