Click here to Skip to main content
15,881,802 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,

Java
"data": [
    {
      "id": 65732147,
      "readable": true,
      "title": "Still into You",
      "title_short": "Still into You",
      "title_version": "",
      "link": "https://www.deezer.com/track/65732147",
      "duration": 216,
      "rank": 755492,
      "explicit_lyrics": false,
      "explicit_content_lyrics": 6,
      "explicit_content_cover": 0,
      "preview": "https://cdns-preview-c.dzcdn.net/stream/c-c83ebbe45ea9bce4eccfc334c9acad9a-14.mp3",
      "md5_image": "d012d587f65e74cacdcce1df0cdef818",
      "artist": {
        "id": 10977,
        "name": "Paramore",
        "link": "https://www.deezer.com/artist/10977",
        "picture": "https://api.deezer.com/artist/10977/image",
        "picture_small": "https://cdns-images.dzcdn.net/images/artist/b176a0784f3d9894bbc2a78d5c0ac8e0/56x56-000000-80-0-0.jpg",
        "picture_medium": "https://cdns-images.dzcdn.net/images/artist/b176a0784f3d9894bbc2a78d5c0ac8e0/250x250-000000-80-0-0.jpg",
        "picture_big": "https://cdns-images.dzcdn.net/images/artist/b176a0784f3d9894bbc2a78d5c0ac8e0/500x500-000000-80-0-0.jpg",
        "picture_xl": "https://cdns-images.dzcdn.net/images/artist/b176a0784f3d9894bbc2a78d5c0ac8e0/1000x1000-000000-80-0-0.jpg",
        "tracklist": "https://api.deezer.com/artist/10977/top?limit=50",
        "type": "artist"
      },
      "album": {
        "id": 6443604,
        "title": "Paramore",
        "cover": "https://api.deezer.com/album/6443604/image",
        "cover_small": "https://cdns-images.dzcdn.net/images/cover/d012d587f65e74cacdcce1df0cdef818/56x56-000000-80-0-0.jpg",
        "cover_medium": "https://cdns-images.dzcdn.net/images/cover/d012d587f65e74cacdcce1df0cdef818/250x250-000000-80-0-0.jpg",
        "cover_big": "https://cdns-images.dzcdn.net/images/cover/d012d587f65e74cacdcce1df0cdef818/500x500-000000-80-0-0.jpg",
        "cover_xl": "https://cdns-images.dzcdn.net/images/cover/d012d587f65e74cacdcce1df0cdef818/1000x1000-000000-80-0-0.jpg",
        "md5_image": "d012d587f65e74cacdcce1df0cdef818",
        "tracklist": "https://api.deezer.com/album/6443604/tracks",
        "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
Model_Music_Results

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("https://deezerdevs-deezer.p.rapidapi.com/search?q="+artist+""),
                Headers =
    {
        { "x-rapidapi-host", "deezerdevs-deezer.p.rapidapi.com" },
        { "x-rapidapi-key", "bb463d49d1msh96af0767769a674p19fc32jsn40fc15ba7a0e" },
    },
            };
            using (var response = await client.SendAsync(request))
            {
                response.EnsureSuccessStatusCode();
                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,
C#
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,
C#
<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.'
</blockquote>
Posted
Updated 14-Oct-21 20: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.
 
Share this answer
 
Comments
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