Hello,
The best way is to copy the json into a C# class which generated container classes to express the json. Also, for some properties in your case have colons in the property names which need to be aliased as shown belowin MusicResuls.
Note this is done with Newtonsoft Json.net NuGet package using .NET 5, C#9 so this line needs to change if using conventional .NET Framework
DownloadedData musicResultsList = new();
Classes
public class DownloadedData
{
public MusicResults results { get; set; }
}
public class MusicResults
{
[JsonProperty("opensearch:Query")]
public OpensearchQuery opensearchQuery { get; set; }
[JsonProperty("opensearch:totalResults")]
public string opensearchtotalResults { get; set; }
[JsonProperty("opensearch:startIndex")]
public string opensearchstartIndex { get; set; }
[JsonProperty("opensearch:itemsPerPage")]
public string opensearchitemsPerPage { get; set; }
public Trackmatches trackmatches { get; set; }
public Attr attr { get; set; }
}
public class OpensearchQuery
{
[JsonProperty("#text")]
public string text { get; set; }
public string role { get; set; }
public string startPage { get; set; }
}
public class Trackmatches
{
public Track[] track { get; set; }
}
public class Track
{
public string name { get; set; }
public string artist { get; set; }
public string url { get; set; }
public string streamable { get; set; }
public string listeners { get; set; }
public Image[] image { get; set; }
public string mbid { get; set; }
}
public class Image
{
public string text { get; set; }
public string size { get; set; }
}
public class Attr
{
}
Then to validate I created a json file, read the information in a class named FileOperations
public static DownloadedData ReadMusicResults(string fileName = "lookup.json")
{
DownloadedData musicResultsList = new();
if (!File.Exists(fileName)) return musicResultsList;
var json = File.ReadAllText(fileName);
musicResultsList = JsonConvert.DeserializeObject<DownloadedData>(json);
return musicResultsList;
}
Used LINQ/Lambda to do a simple query.
var query = FileOperations
.ReadMusicResults()
.results.trackmatches.track
.Where(track => track.artist == "Post Malone").ToList();
Results in the IDE Local window
---