Question: In my C# program I have a JSON file directory that i'veserialized and deserialized into a list from where I'm ableto access variables from the

In my C# program I have a JSON file directory that i'veserialized and deserialized into a list from where I'm ableto access variables from the list and print them out to theconsole but what i'm struggling with is, how to use a for loop todisplay one JSON file at a time so I can ask the user to rate onemovie at a time.

// This program read from a json file to retrive the year andtitle of some movies
// prompting the user to rate them and saving the data back to thejson file.

using System;
using System.Collections.Generic; // for lists
using System.IO; // used for File.ReadAllText
using System.Text.Json;

namespace ConsoleAssignment4
{
class Program
{
static void Main(string[] args)
{
multipleJson();
}

static void multipleJson()
{
string jsonDirectory ="JSON-Movies";

string[] jsonFiles=System.IO.Directory.GetFiles(jsonDirectory); //used for loop whenopening json files
//Console.WriteLine(jsonFiles[2]);

ListMoviesList = new List { };

Movies oneMovies = newMovies();

string jsonData ="";

//loop read files oneby one and copies all the data into the list
foreach(var jsonFile injsonFiles)
{
jsonData =File.ReadAllText(jsonFile); // extracts all text from json andsaves them to a string

oneMovies =JsonSerializer.Deserialize(jsonData);
MoviesList.Add(oneMovies);

}

// print all themovies
Console.WriteLine("themovies are");
foreach (var aMovies inMoviesList)
{
Console.WriteLine(aMovies.title + " (" + aMovies.year + ")");
}


// go through the listone by one change the score
// use a for statement toprint the movies one at a time changing the variables as you go


// print it to theoriginal json
}
}

class Movies
{
public string id { get; set; }
public string title { get; set; }
public string year { get; set; }
public string runtime { get; set;}
public string genre { get; set; }
public string rating { get; set;}
}
}

Note so far I'm able to display the movies title, and year butit displays all of the movies at ounce I',m trying to display oneat a time so I can then ask the user for a rating

Step by Step Solution

3.38 Rating (148 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To display one movie at a time you can use a for loop to iterate through the elements of the MoviesL... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!