可数据词 - json -c#

发布于 2025-01-25 14:54:38 字数 1166 浏览 6 评论 0原文

我有一个类似于

主题问题的数据表qtype
英语xxxxxxx主观
英语yyyyyyy主观
英语zzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzappection
英语sasasas目标
英语cvcvcvvv主观

问题列将包含文本格式的问题。

JSON应该是

{
    "Subject":"English",
    "xxxxxxx":"Subjective",
    "yyyyyyy":"Subjective",
    "zzzzzzz":"Objective",
    "sasasas":"Objective",
    "cvcvcvv":"Subjective"
}

我在下面尝试的。但这不会返回以上输出。

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);

I have a data table like below

SubjectQuestionQtype
EnglishxxxxxxxSubjective
EnglishyyyyyyySubjective
EnglishzzzzzzzObjective
EnglishsasasasObjective
EnglishcvcvcvvSubjective

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

电影里的梦 2025-02-01 14:54:38

由于“主题”仅应使用一次,因此您必须在循环外处理此操作,然后循环循环。
然后使用词典而不是列表:

var dt = new DataTable();
foreach(string c in "Subject,Question,Qtype".Split(','))
    dt.Columns.Add(c, typeof(string));
    
for(int i = 1; i<= 5;i++)
    dt.Rows.Add("English", $"Q{i}", (i==3 || i==4 ? "Objective" : "Subjective"));   

var dc = new Dictionary<string, string>();

dc.Add("Subject", dt.Rows[0].Field<string>("Subject"));

foreach(DataRow r in dt.Rows)
{
    dc.Add(r.Field<string>("Question"), r.Field<string>("QType"));
}

var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(dc);

或使用newtonsoft:

string json2 = Newtonsoft.Json.JsonConvert.SerializeObject(dc);

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:

var dt = new DataTable();
foreach(string c in "Subject,Question,Qtype".Split(','))
    dt.Columns.Add(c, typeof(string));
    
for(int i = 1; i<= 5;i++)
    dt.Rows.Add("English", 
quot;Q{i}", (i==3 || i==4 ? "Objective" : "Subjective"));   

var dc = new Dictionary<string, string>();

dc.Add("Subject", dt.Rows[0].Field<string>("Subject"));

foreach(DataRow r in dt.Rows)
{
    dc.Add(r.Field<string>("Question"), r.Field<string>("QType"));
}

var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(dc);

Or with Newtonsoft:

string json2 = Newtonsoft.Json.JsonConvert.SerializeObject(dc);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文