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.

enter image description here

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 } } } 
  • no json example no one will tell you. And no matter where he comes from. - vitidev
  • why throw here. edit the question where put the readable json - vitidev
  • you have some kind of incorrect JSON - why after url or photo goes ; ? And the array is not closed - Grundy
  • Actually, there is a colon, I copied it from VC Deva, where there is an analysis of methods, there is an array, which is what the audio.get method gives - Dima Potapov
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

If we assume that the resulting json is correct, then the reason is incorrect processing.

  1. JToken and other classes from this library already implement IEnumerable so extension methods can be called right away, not through a static class.

  2. To get an array of items you just need to take it.

     var items = token["response"]["items"]; 
  3. Then apply the Select method to it.


@ DimaPotapov, you tried incorrectly. In your case, in a select, you run through a sequence with one element, which is an array - and you wanted to deserialize this array into an Audio class object, and, naturally, received an error

  • JToken token = JToken.Parse (responseFromServer); audioList = token ["response"]. Children (). Skip (1) .Select (c => c.ToObject <Audio> ()). ToList (); - Dima Potapov
  • This is how I tried, but it still makes me a mistake - Dima Potapov
  • Anons of type Newtonsoft.Json.JsonSerializationException in Newtonsoft.Json.dll but it wasn’t even deserializing object: PropertyName. Path '', line 1, position 33. - Dima Potapov
  • @ DimaPotapov, supplemented the answer - Grundy