【发布时间】:2019-12-23 03:17:30
【问题描述】:
我正在使用 asp.net c# 构建一个 Web 表单来生成 JSON 字符串。我有一个 SQL Server 表,其中包含列 ID、列名、父 ID、值。
对于特定 ID,如果 Id = parentId 则它是父列,而如果 id != parentid 则它是子列并且有父列。我将这些拉到一个数据表中,我需要在标签中打印一个 JSON 字符串(如实际输出中所示)
我尝试如下使用 Newtonsoft.Json
foreach (DataRow row in JSONInfoTable.Rows)
{
string result = Newtonsoft.Json.JsonConvert.serializeObject(row);
testlabel.Text = result;
}
但是我从上面的代码中得到了下面的结果
{"Columnname":"Parent1","Value:100"}...
实际输出 JSON(在 btnclick 事件后打印在 Webform 标签中)
{
"Parent1" : "100",
"Parent2" : {
"child1" : "300",
"child2" : "40"
}
}
SQL Server:GetJSON 表
Id Columnname ParentId Value
----------------------------------
1 Parent1 1 100
2 Parent2 2 NULL
3 Child1 2 300
4 Child2 2 40
C# 数据表(从 GetJSON 表中提取数据)
SqlCommand Fieldextract = new SqlCommand(Select Columnname, Value from GetJSON, sqlcon);
Dataset ds1 = new DataSet();
SqlDataAdapter = JSONInfoAdapter ;
JSONInfoAdapter = new SQLDataAdapter(FieldExtract);
JSONInfoAdapter.fill(ds1);
FieldExtract.Dispose();
JSONInfoAdapter = null;
DataTable JSONInfoTable = ds1.Table[0];
【问题讨论】: