-1

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; }
}
Chamkey
  • 105
  • 9

1 Answers1

1

From the examples you provided I saw some problems. Firstly the JSON for StudentNo contains a ":" which could cause some problems when deserializing so I would advice you to remove those.

I tried your example and changed it a bit. The DataTable should incorporate very easy into your project since a Dictionary isn't the best choice.

    public static DataTable DoSomething() 
    {
        DataTable table = new DataTable();
        table.Columns.Add("Student Name");
        table.Columns.Add("Student Number");
        table.Columns.Add("Registered");

        getJsonData data = new JavaScriptSerializer().Deserialize<getJsonData>(System.IO.File.ReadAllText(@"C:\Users\mailb_000\Downloads\texts\test.json"));

        foreach (var item in data.students)
        {
            table.Rows.Add(item.Name, item.StudentNo, item.registered);
            Debug.Write(table);
        }

        return table;
    }

    public class getJsonData
    {
        public List<students> students { get; set; }
    }


    public class students
    {

        public string Name { get; set; }

        public int StudentNo { get; set; }

        public string registered { get; set; }
    }

BTW. Since you wrote you want a HTML table I guess this link should help you Datatable to html Table

Community
  • 1
  • 1
Bongo
  • 2,933
  • 5
  • 36
  • 67