gpt4 book ai didi

c# - 在 JSON 对象的属性中存储 JSON 对象

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:31 25 4
gpt4 key购买 nike

如何在 JSON 对象的属性中存储 JSON 对象,我的意思是 JSON 对象的属性值是另一个 JSON 对象。我有一个 DataTable dt,我从 DataBase 读取数据并将其存储在这个 DataTable 中。

using (var da = new SqlDataAdapter(command))
{
command.CommandType = CommandType.StoredProcedure;
da.Fill(dt);
}

现在,我向这个 DataTable 添加一列

dt.Columns.Add("attributes");

现在,我创建一个 JObject 并将其值存储在 DataTable 每一行的“属性”列中

dynamic attributeValue = new JObject();
attributeValue.type = "Stage_FF_Hot_Alerts__c";

foreach (DataRow d in dt.Rows)
{
d["attributes"] = attributeValue;
}

现在,我序列化这个数据表

string JSONresult = JsonConvert.SerializeObject(dt);

我得到的结果是

{
"attributes" : "{"type" : "Stage_FF_Hot_Alerts__c"}",
"Address__c" : "Street",
"AgentID__c" : "123456",
"Alert_Status__c" : "Closed",
"BusinessUnit__c" : "INFINITI",
"Case_Type__c" : "INFINITI Service",
"City__c" : "City",
"ContactId__c" : "10951",
"DayTimePhone__c" : "123456789",
"DealerCode__c" : "72067",
"DealerName__c" : "Infiniti Of Kansas City",
"EmailAddress__c" : "INF@isky.com",
"EveningPhone__c" : "123456789",
"FirstName__c" : "CustomerFirstName",
"HotAlertType__c" : "Hot Alert",
"LastName__c" : "CustomerSurname",
"NPS_Score_1__c" : "0",
"V01_Alert_Trigger__c" : "Which of the following best describes your overall service experience?",
"Field_Open_Date__c" : "2018-08-05"
}

而我想要的结果是

{
"attributes" : {"type" : "Stage_FF_Hot_Alerts__c"},
"Address__c" : "Street",
"AgentID__c" : "123456",
"Alert_Status__c" : "Closed",
"BusinessUnit__c" : "INFINITI",
"Case_Type__c" : "INFINITI Service",
"City__c" : "City",
"ContactId__c" : "10951",
"DayTimePhone__c" : "123456789",
"DealerCode__c" : "72067",
"DealerName__c" : "Infiniti Of Kansas City",
"EmailAddress__c" : "INF@isky.com",
"EveningPhone__c" : "123456789",
"FirstName__c" : "CustomerFirstName",
"HotAlertType__c" : "Hot Alert",
"LastName__c" : "CustomerSurname",
"NPS_Score_1__c" : "0",
"V01_Alert_Trigger__c" : "Which of the following best describes your overall service experience?",
"Field_Open_Date__c" : "2018-08-05"
}

最佳答案

示例代码中的以下行在未指定数据类型的情况下在 DataTable 中创建列,因此类型 defaults to string .

dt.Columns.Add("attributes");

尝试使用指定您想要的类型的重载,例如:

dt.Columns.Add("attributes", typeof(object));

或者也许:

dt.Columns.Add("attributes", typeof(JObject));

关于c# - 在 JSON 对象的属性中存储 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54142486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com