我正在研究wp app。我想我的json数据类型有问题,但我不确定。首先,我想分享我的json数据示例:
[{"Id":2,"PoemName":"Necip Fazıl Kısakürek","PoemImage":null,"Biography":null},{"Id":1,"PoemName":"Orhan Veli Kanık","PoemImage":"deneme","Biography":"asda"}]
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数据类型存在问题,但我不知道如何更改此类型。
我希望我能解释一下。感谢所有回复。
答案 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);