JsonSerializationException how to solve

61 Views Asked by At

I have been trying deserialiable the api and the program shows this error: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[System.Diagnostics.Activity]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'activity', line 1, position 12.'

This is my code: enter image description here enter image description here

thanks if someone can help me

1

There are 1 best solutions below

0
Fazle Rabbi On

if your json data looks like this :

     jsonData = [{"id":"518523721","name":"ftyft"},

              {"id":"527032438","name":"ftyftyf"},

              {"id":"527572047","name":"ftgft"},

              {"id":"531141884","name":"ftftft"}]

then you can try :

    public class Person
     
    {
     
         public string Id { get; set; }
    
         public string Name { get; set; }
     
    }
    
     
    var people = JsonConvert.DeserializeObject<List<Person>>(json);

in case of array format :

    public class PersonArray
     
    {          

        public Pserson[] Person{get;set;}
     
    }

  var people = JsonConvert.DeserializeObject<PersonArray>(json);