Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I can't seem to figure out why my JSON code, will not print new objects in the new directory when it is made. Everything seems
I can't seem to figure out why my JSON code, will not print new objects in the new directory when it is made. Everything seems to work besides the serialization. Code: using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
public class TvShow
public int Id get; set;
public string BackdropPath get; set;
public string Name get; set;
public string OriginCountry get; set;
public string OriginalLanguage get; set;
public string OriginalName get; set;
public string Overview get; set;
public double Popularity get; set;
public string PosterPath get; set;
public double VoteAverage get; set;
public int VoteCount get; set;
public class TvShowWriter
public string BaseDirPath get; set;
public string WriteDirPath get; set;
public TvShowWriterstring baseDirectory, string writeDirectoryPath
BaseDirPath baseDirectory;
WriteDirPath writeDirectoryPath;
Create WriteDirPath directory if it doesn't exist
if Directory.ExistsWriteDirPath
Directory.CreateDirectoryWriteDirPath;
public void MoveToBaseDir
Directory.SetCurrentDirectoryBaseDirPath;
public void WriteTvShow tvShow
Write TvShow details to a text file
string filePath Path.CombineWriteDirPath $tvShowNametxt;
using StreamWriter writer File.CreateTextfilePath
writer.WriteLine$"Name: tvShowName;
writer.WriteLine$"Origin Country: tvShowOriginCountry;
Write other properties as needed
public void WriteToJsonTvShow tvShow
Serialize TvShow object to JSON
string json JsonSerializer.SerializetvShow;
Write JSON to a file
string filePath Path.CombineWriteDirPath $tvShowNamejson";
File.WriteAllTextfilePath json;
public int CreateCountryDirectoriesList tvShows, string countryDirName, bool returnToBasePath true
int count ;
string writePathDir Path.CombineWriteDirPath countryDirName;
Create directory if it doesn't exist
if Directory.ExistswritePathDir
Directory.CreateDirectorywritePathDir;
count;
Get unique countries from tvShows
var uniqueCountries tvShows.Selectt tOriginCountryDistinct;
Create directory for each country
foreach var country in uniqueCountries
string countryPath Path.CombinewritePathDir country;
if Directory.ExistscountryPath
Directory.CreateDirectorycountryPath;
count;
if returnToBasePath
MoveToBaseDir;
return count;
public void WriteShowsByCountryList tvShows, string countryDirName, bool returnToBasePath true
string countryWritePathDir Path.CombineWriteDirPath countryDirName;
Create directory if it doesn't exist
if Directory.ExistscountryWritePathDir
Directory.CreateDirectorycountryWritePathDir;
Get unique countries from tvShows
var uniqueCountries tvShows.Selectt tOriginCountryDistinct;
foreach var country in uniqueCountries
string countryPath Path.CombinecountryWritePathDir country;
Create directory if it doesn't exist
if Directory.ExistscountryPath
Directory.CreateDirectorycountryPath;
Write TvShows corresponding to each country
var showsInCountry tvShows.Wheret tOriginCountry country;
foreach var show in showsInCountry
string filePath Path.CombinecountryPath $showNametxt;
using StreamWriter writer File.CreateTextfilePath
writer.WriteLine$"Name: showName;
writer.WriteLine$"Origin Country: showOriginCountry;
Write other properties as needed
if returnToBasePath
MoveToBaseDir;
class Program
static void Mainstring args
Prompt : Initialize TvShowWriter
string baseDir AppDomain.CurrentDomain.BaseDirectory;
string writeDir @"TvShows";
TvShowWriter writer new TvShowWriterbaseDir writeDir;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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