Click here to Skip to main content
15,119,786 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all i have been trying now for like 4 days to get my json array returnd from a api to a list of object and store them in a list<>

so say i had a list<> elfenlist = new List<>

then had the following,
json returned from a api,

"data": [
      "id": 65732147,
      "readable": true,
      "title": "Still into You",
      "title_short": "Still into You",
      "title_version": "",
      "link": "",
      "duration": 216,
      "rank": 755492,
      "explicit_lyrics": false,
      "explicit_content_lyrics": 6,
      "explicit_content_cover": 0,
      "preview": "",
      "md5_image": "d012d587f65e74cacdcce1df0cdef818",
      "artist": {
        "id": 10977,
        "name": "Paramore",
        "link": "",
        "picture": "",
        "picture_small": "",
        "picture_medium": "",
        "picture_big": "",
        "picture_xl": "",
        "tracklist": "",
        "type": "artist"
      "album": {
        "id": 6443604,
        "title": "Paramore",
        "cover": "",
        "cover_small": "",
        "cover_medium": "",
        "cover_big": "",
        "cover_xl": "",
        "md5_image": "d012d587f65e74cacdcce1df0cdef818",
        "tracklist": "",
        "type": "album"

the following is the classmodel,
<pre lang="C#">        public class Rootobject
            public Datum[] data { get; set; }
        public class Datum
            public int id { get; set; }
            public bool readable { get; set; }
            public string title { get; set; }

            public string title_short { get; set; }
            public string title_version { get; set; }
            public string link { get; set; }
            public int duration { get; set; }
            public int rank { get; set; }
            public bool explicit_lyrics { get; set; }
            public int explicit_content_lyrics { get; set; }
            public int explicit_content_cover { get; set; }
            public string preview { get; set; }
            public string md5_image { get; set; }

            public Artist artist { get; set; }
            public Album album { get; set; }
            //public Artist artist { get; set; }
            //public Album album { get; set; }
        public class Artist
            public int id { get; set; }
            public string name { get; set; }
            public string link { get; set; }
            public string picture { get; set; }
            public string picture_small { get; set; }
            public string picture_medium { get; set; }
            public string picture_big { get; set; }
            public string picture_xl { get; set; }
            public string tracklist { get; set; }
            public string type { get; set; }
        public class Album
            public int id { get; set; }
            public string title { get; set; }
            public string cover { get; set; }
            public string cover_small { get; set; }
            public string cover_medium { get; set; }
            public string cover_big { get; set; }
            public string cover_xl { get; set; }
            public string md5_image { get; set; }
            public string tracklist { get; set; }
            public string type { get; set; }

and what i want the list to do is store all the values listed in the

that is the name of the file classmodel,

then i have the following function to grab all the data,

<pre lang="C#"><pre> public class API_Music_Artists : Project_Yuno_Res
        public static async Task<string> gendataAsync(string artist)
            var client = new HttpClient();
            var request = new HttpRequestMessage
                Method = HttpMethod.Get,
                RequestUri = new Uri(""+artist+""),
                Headers =
        { "x-rapidapi-host", "" },
        { "x-rapidapi-key", "bb463d49d1msh96af0767769a674p19fc32jsn40fc15ba7a0e" },
            using (var response = await client.SendAsync(request))
                var body = await response.Content.ReadAsStringAsync();
                //musicvals = JsonConvert.DeserializeObject<List<Rootobject>>(body);
                var des = (Rootobject)JsonConvert.DeserializeObject(body, typeof(Rootobject));
                //return des..Count.ToString();
                return "";
               var elfenreponce = JsonConvert.DeserializeObject<List<DataModel>>(body);
        public static string makenicejson(string responce)
            JToken parseJason = JToken.Parse(responce);
            return parseJason.ToString(Formatting.Indented);


and the methord i use to call from the form is,
API_Music_Artists elfenapi = new API_Music_Artists();
Project_Yuno_Res.Artist_Search = txt_artist.Text;
var elfen = await API_Music_Artists.gendataAsync(Project_Yuno_Res.Artist_Search);

What I have tried:

//musicvals = JsonConvert.DeserializeObject<List<Rootobject>>(body);

also tryied to pass the model in derictly

public static async Task<Model_Music_Results> gendataAsync(string artist)

tried the deserialize into a list itself by,
musicvals = JsonConvert.DeserializeObject<List<Rootobject>>(body)

but i get the following error,
<blockquote class="quote"><div class="op">Quote:</div>Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Yuno_Res.Model_Music_Results+Rootobject]' 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<T>) 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 'data', line 1, position 8.'
Updated 14-Oct-21 21:07pm

1 solution

Your JSON either isn't complete, or isn't valid as shown: it is at the very least missing the the opening "{" and some "}]}" terminators.
Without those, the JsonConvert.Deserialize method can't work - it doesn't know what to do with the data, and for good reasons won't guess what is missing.

So start with your data and the source: check it's valid and if not find out why and how "reliable" the error is. Then get that fixed before you go any further.

When you have valid Json, compare the classes it contains with your classes - I use Convert JSON to C# Classes Online - Json2CSharp Toolkit[^] myself - and then start trying to code with it.

But invalid data gives invalid results - so start with that and work from there.
mion shion 17-Oct-21 16:01pm
OrignalGriff - thank you very much i did make a mistake and did not include the very top that api returned 230 results so was not going to post it all in here but the website you provided worked perfectly and i now have working app thank you very much for your responce
OriginalGriff 17-Oct-21 16:38pm
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900