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
Step: 1
To display one movie at a time you can use a for loop to iterate through the elements of the MoviesL...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started