Click here to Skip to main content
14,927,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
            public OpensearchQuery opensearchQuery { get; set; }
            public string opensearchtotalResults { get; set; }
            public string opensearchstartIndex { get; set; }
            public string opensearchitemsPerPage { get; set; }
            public Trackmatches trackmatches { get; set; }
            public Attr attr { get; set; }

        public class OpensearchQuery
            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 = ""+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);
            // Json file by default is one line lets get the formatting correct 

            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");


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()
            // now lets look up the song in question, //*FileOperations

            var query = FileOperations
           .ReadMusicResults(Application.StartupPath + "//lookup.json")
           .Where(track => == "Rockstar").ToList();

What I have tried:

have tryied creating a new instance of track elfen = new track() 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.
Updated 22-Apr-21 23:28pm
Richard Deeming 23-Apr-21 4:43am
NB: It's "LINQ", not "line Q".

1 solution

have tryied creating a new instance of track elfen = new track() but it returns null,
It will do - you are creating a new instance of the track class, and that means everything in the instance will be empty.

Since you read the data in your runResults method, but then discard everything the Linq query builds by storing it in a local variable which immediately gets discarded you can't use any of it outside the method.

Start simply, and use the debugger to look at what you are working with:
var fromFile = FileOperations.ReadMusicResults(Application.StartupPath + "//lookup.json");
var results = fromFile.results;
var trackmatches = results.trackmatches;
var track = trackmatches.track;
var list = track.Where(track => == "Rockstar").ToList();
Put a breakpoint on the first line, and step though, looking at the results generated each time.

We can't do that for you - we dont have access to your data!

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