Wp8 Json反序列化问题

时间:2014-02-13 19:09:55

标签: windows-phone-8 asp.net-web-api json json-deserialization

我正在研究wp app。我想我的json数据类型有问题,但我不确定。首先,我想分享我的json数据示例:

Json Data

[{"Id":2,"PoemName":"Necip Fazıl Kısakürek","PoemImage":null,"Biography":null},{"Id":1,"PoemName":"Orhan Veli Kanık","PoemImage":"deneme","Biography":"asda"}]

OnLoad事件

var webClient = new WebClient();
        webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
        webClient.DownloadStringAsync(new Uri("http://denememvc-001-site1.smarterasp.net/api/poem"));

另一个功能

void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        var results = JsonConvert.DeserializeObject<ToList>(e.Result);
        MessageBox.Show(results.ToString());
    }

    public class Poem
    {
        public int Id { get; set; }
        public string PoemName { get; set; }
        public string PoemImage { get; set; }
        public string Biography { get; set; }
    }

    public class ToList
    {
        public List<Poem> poems { get; set; }
    }

之后它给出了这个错误:“附加信息:无法将当前JSON数组(例如[1,2,3])反序列化为类型'SiirYuvasi.TrialWebSide + ToList',因为该类型需要一个JSON对象(例如{”名称“:”值“})正确反序列化。”

正如我所说,我认为我的json数据类型存在问题,但我不知道如何更改此类型。

我希望我能解释一下。感谢所有回复。

2 个答案:

答案 0 :(得分:0)

尝试使用Array代替List<>

public class ToList
{
    public Poem[] poems { get; set; }
}

答案 1 :(得分:0)

试试这个。

public class Poem
{
//getters
//setters
}

public class ToList
{
    public Poem poem { get; set; }
}

var result = JsonConvert.DeserializeObject<List<ToList>>(e.Result);