Deserialze JSON arrays with Json.Net returning blank spaces when called to print to screen

  • Thread starter Thread starter Rechtfertigung
  • Start date Start date
R

Rechtfertigung

Guest
Im trying to use Json.Net to deserialize a JSON file I made, I used the VS built in JSON Class formatter to make a class structure relative to my JSON objects and arrays. However, when I go to print the names of the Employees to the screen all I get are blank spaces inside the console application window. I've tried using a List<T> instead of an array but still the same problem. Also I thought it might be a problem with my JSON file, like not putting a correct "," or ":" but I used a JSON validator which said the file was valid. P.s fairly new to using Json and Json.Net so it might be a simple fix to some.

code for deserializing and printing to screen:

string FactoryWorkersInfo = File.ReadAllText("FactoryWorkers.json");
Employee Workers = new Employee();
Workers = JsonConvert.DeserializeObject<Employee>(FactoryWorkersInfo);
Employee[] employees = new Employee[7];
foreach(var name in employees)
{
Console.WriteLine(name);


Code for the classes:

public class Rootobject
{
public string OrganizationName { get; set; }
public Employee[] Employees { get; set; }
}

public class Employee
{
public string Name { get; set; }
public int Age { get; set; }
public bool Present { get; set; }
}

JSON:

{
"OrganizationName": "Factory Clean up",
"Employees": [
{
"Name": "Jacob Reed",
"Age": 24,
"Present": false
},
{
"Name": "Andrew Eust",
"Age": 54,
"Present": true
},
{
"Name": "Tanya Fernandez",
"Age": 24,
"Present": true
},
{
"Name": "Devin Burns",
"Age": 36,
"Present": true
},
{
"Name": "Kevin Gardner",
"Age": 21,
"Present": false
},
{
"Name": "Isabel Logan",
"Age": 45,
"Present": true
},
{
"Name": "Velma Sparks",
"Age": 30,
"Present": true
},
{
"Name": "Nichole Joseph",
"Age": 32,
"Present": true
}
]
}

Continue reading...
 
Back
Top