Good Morning/Afternoon,
i have a json file that is parsed I currently am using lineq to access the data in the json file but would like to create a list of all objects and be able to loop through all the data in there so i can find certain name or track,
code i currently have,
the class to deserialize the json file,
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
{
}
the search code,
<pre> string lookup = "http://ws.audioscrobbler.com/2.0/?method=track.search&track="+track+"&api_key=APIKEY&format=json";
string json = string.Empty; using (var client = new WebClient()) { json = await client.DownloadStringTaskAsync(lookup); }
File.WriteAllText(Application.StartupPath + "//lookup.json", json, Encoding.UTF8);
string jsonfix = File.ReadAllText(Application.StartupPath + @"\lookup.json");
JToken jt = JToken.Parse(jsonfix);
string formatted = jt.ToString(Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(Application.StartupPath + @"\lookup.json", formatted);
FileOperations.ReadMusicResults(Application.StartupPath + "\\lookup.json");
runresults();
then the FileOperations Class,
<pre> class FileOperations
{
public static DownloadedData ReadMusicResults(string fileName)
{
DownloadedData musicResultsList = new DownloadedData();
if (!File.Exists(fileName)) return musicResultsList;
var json = File.ReadAllText(fileName);
musicResultsList = JsonConvert.DeserializeObject<DownloadedData>(json);
return musicResultsList;
}
}
then finally the lineQ code i using currently does work but unable to access it from other parts trying to acess the track class or the list of objects it returns form the line q currently returns 12 in the list,
looking for a way to loop through the list in other classes but lost on how to create a global list<>
lineq currently using,
<pre lang="C#"><pre> public void runresults()
{
var query = FileOperations
.ReadMusicResults(Application.StartupPath + "//lookup.json")
.results.trackmatches.track
.Where(track => track.name == "Rockstar").ToList();
}
https://pastebin.com/geNfV96h
What I have tried:
have tryied creating a new instance of track elfen = new track()
elfen.name but it returns null,
was hoping it would store the some of the data or least last in the list but returns null,
any help would be much appriacted.