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": "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 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();
var des = (Rootobject)JsonConvert.DeserializeObject(body, typeof(Rootobject));
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:
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.'
</blockquote>