When receiving a response from the VK API and converting JSON to List<Audio> , the following error appears:
Anons of type 'Newtonsoft.Json.JsonSerializationException' in Newtonsoft.Json.dll but it was not handled in user code
Additional information: Unexpected token while deserializing object: PropertyName. Path '', line 1, position 33.
How to convert JSON to List<Audio> correctly?
Here is the code:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { new Form2().Show(); backgroundWorker1.RunWorkerAsync(); } public List<Audio> audioList = null; public class Audio { public int id { get; set; } public int owner_id { get; set; } public string artist { get; set; } public string title { get; set; } public int duration { get; set; } public string url { get; set; } public string lyrics_id { get; set; } public int genre_id { get; set; } //id: 232745053, //owner_id: 34, //artist: 'Ambassadeurs', //title: 'Sparks', //duration: 274, //url: 'http://cs6164.vk....M_lGEJhqRK8d5OQZngI', //lyrics_id: 120266970, //genre_id: 18 } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { while (!Settings1.Default.auth) { Thread.Sleep(500); } WebRequest request = WebRequest.Create("https://api.vk.com/method/audio.get?user_id=" + Settings1.Default.user_ID + "&v=5.50&need_user=0&access_token=" + Settings1.Default.user_token); WebResponse response = request.GetResponse(); Stream dataStream = response.GetResponseStream(); StreamReader dataReader = new StreamReader(dataStream); string responseFromServer = dataReader.ReadToEnd(); dataReader.Close(); dataStream.Close(); response.Close(); responseFromServer = HttpUtility.HtmlDecode(responseFromServer); //MessageBox.Show(responseFromServer); JToken token = JToken.Parse(responseFromServer); audioList = Enumerable.Skip(token["response"].Children(), 1).Select(c => c.ToObject<Audio>()).ToList(); this.Invoke((MethodInvoker)delegate { for (int i = 0; i < audioList.Count(); i++) { listBox1.Items.Add(audioList[i].artist + " - " + audioList[i].title); } }); } } Json:
{ response: { count: 505, items: [ { id: '34', photo: 'http://cs7009.vk....3f2/rj4RvYLCobY.jpg', name: 'Tatyana Plutalova', name_gen: 'Tatyana' }, { id: 232745053, owner_id: 34, artist: 'Ambassadeurs', title: 'Sparks', duration: 274, url: 'http://cs6164.vk....M_lGEJhqRK8d5OQZngI', lyrics_id: 120266970, genre_id: 18 }, { id: 232733966, owner_id: 34, artist: 'Aloe Blacc', title: 'Can You Do This ', duration: 176, url: 'http://cs6157.vk....ZaerOa0DvsyOCYTPO1w', genre_id: 2 } } } 
urlorphotogoes;? And the array is not closed - Grundy