从动态 ExpandoObject 创建 JSON 数组
Creating JSON array from dynamic ExpandoObject
我正在使用 C# 并尝试从动态对象创建 JSON 字符串。
string jsonObj = string.Empty;
dynamic DetailObj = new ExpandoObject();
foreach (DataRow row in dtDetails.Rows)
{
DetailObj.ID = row["ID"].ToString();
DetailObj.Description = row["Description"].ToString();
DetailObj.ExpDate = row["Date"].ToString();
}
dynamic EmailObj = new ExpandoObject();
foreach (DataRow row in dtReturnData.Rows)
{
EmailObj.ID = row["EmailTypeId"].ToString();
EmailObj.FirstName = row["FirstName"].ToString();
EmailObj.LastName = row["LastName"].ToString();
EmailObj.Details = DetailObj;
EmailObj.Email = row["Email"].ToString();
}
jsonObj = JsonConvert.SerializeObject(EmailObj);
我正在尝试获取输出字符串:
[{"ID":"5","FirstName":"Joe","LastName":"Johnson","Details":[{"ID":"1","Description":"Player1","ExpDate":"08/30/2021
00:00:00"}],"Email":"joejohnson@sample.com"}]
但我目前得到的是:
"{"ID":"5","FirstName":"Joe","LastName":"Johnson","Details":{"ID":"1","Description":"Player1","ExpDate":"08/30/2021
00:00:00"},"Email":"joejohnson@sample.com"}"
想要的输出和当前输出的区别在于整个JSON开头和结尾的[],而不是“”,还有[=26=里面的detailobj对象].
如何更改输出?
此外,dtDetails 包含多行,所有这些行都需要在 JSON 详细信息对象中传递。目前,我得到作为 DetailObj 的一部分传递的单行。有办法纠正吗?
问题是您在迭代时覆盖了所有这些字段的值。你想要做的是有一个动态对象列表。例如:
List<dynamic> EmailObj = new List<ExpandoObject>();
foreach (DataRow row in dtReturnData.Rows)
{
var item = new ExpandoObject();
item.ID = row["EmailTypeId"].ToString();
item.FirstName = row["FirstName"].ToString();
item.LastName = row["LastName"].ToString();
item.Details = DetailObj;
item.Email = row["Email"].ToString();
EmailObj.Add(item);
}
我正在使用 C# 并尝试从动态对象创建 JSON 字符串。
string jsonObj = string.Empty;
dynamic DetailObj = new ExpandoObject();
foreach (DataRow row in dtDetails.Rows)
{
DetailObj.ID = row["ID"].ToString();
DetailObj.Description = row["Description"].ToString();
DetailObj.ExpDate = row["Date"].ToString();
}
dynamic EmailObj = new ExpandoObject();
foreach (DataRow row in dtReturnData.Rows)
{
EmailObj.ID = row["EmailTypeId"].ToString();
EmailObj.FirstName = row["FirstName"].ToString();
EmailObj.LastName = row["LastName"].ToString();
EmailObj.Details = DetailObj;
EmailObj.Email = row["Email"].ToString();
}
jsonObj = JsonConvert.SerializeObject(EmailObj);
我正在尝试获取输出字符串:
[{"ID":"5","FirstName":"Joe","LastName":"Johnson","Details":[{"ID":"1","Description":"Player1","ExpDate":"08/30/2021 00:00:00"}],"Email":"joejohnson@sample.com"}]
但我目前得到的是:
"{"ID":"5","FirstName":"Joe","LastName":"Johnson","Details":{"ID":"1","Description":"Player1","ExpDate":"08/30/2021 00:00:00"},"Email":"joejohnson@sample.com"}"
想要的输出和当前输出的区别在于整个JSON开头和结尾的[],而不是“”,还有[=26=里面的detailobj对象].
如何更改输出?
此外,dtDetails 包含多行,所有这些行都需要在 JSON 详细信息对象中传递。目前,我得到作为 DetailObj 的一部分传递的单行。有办法纠正吗?
问题是您在迭代时覆盖了所有这些字段的值。你想要做的是有一个动态对象列表。例如:
List<dynamic> EmailObj = new List<ExpandoObject>();
foreach (DataRow row in dtReturnData.Rows)
{
var item = new ExpandoObject();
item.ID = row["EmailTypeId"].ToString();
item.FirstName = row["FirstName"].ToString();
item.LastName = row["LastName"].ToString();
item.Details = DetailObj;
item.Email = row["Email"].ToString();
EmailObj.Add(item);
}