I have a data table like below
| Subject | Question | Qtype |
|---|---|---|
| English | xxxxxxx | Subjective |
| English | yyyyyyy | Subjective |
| English | zzzzzzz | Objective |
| English | sasasas | Objective |
| English | cvcvcvv | Subjective |
Question column will contain the question in text format.
Json should be
{
"Subject":"English",
"xxxxxxx":"Subjective",
"yyyyyyy":"Subjective",
"zzzzzzz":"Objective",
"sasasas":"Objective",
"cvcvcvv":"Subjective"
}
I have tried like below. But this will not return above output.
var list = new List<string[]>();
foreach (DataRow row in dt_questions.Rows)
{
string Subject = row["Subject"].ToString();
string Question= row["Question"].ToString();
string Qtype= row["Qtype"].ToString();
list.Add(new string[] { Subject, Question, Qtype});
}
var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(list);
Since the 'subject' should be used only once, you have to treat this outside the loop, and loop over the rest.
Then use a dictionary instead of a list:
Or with Newtonsoft: