I have the following happening inside my C# MVC application in a controller.
Dictionary<string, string> dictionaryList = new Dictionary<string, string>();
getJsonData data = new JavaScriptSerializer().Deserialize<getJsonData>(System.IO.File.ReadAllText(json_file_path));
foreach (var item in data.students)
{
dictionaryList.Add("Student Name: ", item.studName);
dictionaryList.Add("Student Number: ", item.studNumb);
dictionaryList.Add("Registered: ", item.registered);
Debug.Write(dictionaryList);
}
First and foremost in my DebugConsole I'm getting
System.Collections.Generic.Dictionary`2[System.String,System.String]
instead of a value
and on the second iteration it throws an exception
Exception thrown: 'System.ArgumentException' in mscorlib.dll
I know that the second iteration is throwing the exception because dictionaryList can only have one unique key at a time but looking through the forums I'm a little stumped as to how to implement some of the solutions being given. The code functions normally if I remove the dictionary and just Debug.Write(data.jobs);
but it's easier to move the data over to an HTML table if it's in dictionary format.
I cannot use
List<Students> = JsonConvert.DeserializeObject<getJsonData>(System.IO.File.ReadAllText(json_file_path));
because my json file is in the format
{
"students": [{
"Name" : "Robert Mcguffin",
"Registered" : "2014-07-20 05:34:16",
"StudentNo:" : 1
} , {
"Name" : "Agathe Dubois",
"Registered" : "2014-05-30 09:46:26",
"StudentNo:" : 2
} , {
"Name" : "Steven Corral",
"Registered" : "2015-02-11 09:58:16",
"StudentNo:" : 3
}]
}
and is not recognized by JsonConvert for some reason.
I would really like to know how to put my data into the dictionary using the foreach loop.
Thank you.
public class getJsonData
{
public List<Students> students { get; set; }
}
public class students
{
public string studName { get; set; }
public string studNumb { get; set; }
public string registered { get; set; }
}