I have a JSON document that I'm trying to deserialize. This is a document that may changed depending on what information is available on certain products. So if there is a product with no downloadable files, there will be missing JSON objects and I need to know if I return null or not. As of now, my application will crash if its null and I don't know how to fix it.
using (WebClient wc = new WebClient())
{
wc.Headers.Add("User-Agent", "C# Windows Application");
String jsonData = wc.DownloadString(URL);
EfobasenRoot EfobasenDeserialized = JsonConvert.DeserializeObject<EfobasenRoot>(jsonData);
// EL Nr
elNummer = EfobasenDeserialized.Produktskjema.Produktnr;
// Varetekst
vareTekst = EfobasenDeserialized.Produktinfo.Varetekst;
// Fabrikat
fabrikat = EfobasenDeserialized.Produktinfo.Fabrikat;
// FDV Download ID
// Due to syntax in the Json file, I take the last bbject and save it to a string
// So we can deserialize it again to get the fileID for the FDV file
jsonFDV = EfobasenDeserialized.Produktskjema.Skjema.Grupper[2].Felter[0].Verdi.ToString();
EfobasenFDV EfobasenFDVDeserialized = JsonConvert.DeserializeObject<EfobasenFDV>(jsonFDV);
fdvNummer = Convert.ToInt32(EfobasenFDVDeserialized.FilId);
// Download the FDV file from URL
var saveFile = new SaveFileDialog();
saveFile.FileName = fabrikat + "-" + vareTekst + "-" + elNummer + "-FDV";
saveFile.Filter = "PDF document (*.pdf)|*.pdf";
var result = saveFile.ShowDialog();
if (result == DialogResult.OK)
{
wc.DownloadFile(fdvDownloadPath + fdvNummer, saveFile.FileName);
}
}
In this code, I need to check "jsonFDV" is empty or not. If it return null now it crashes I don't know how to validate it.