【发布时间】:2019-08-04 04:21:46
【问题描述】:
我有一个列表类List<Data> dataValue = new List<Data>,其中数据包含目标列表(多个)和源列表(多个)详细信息。我想遍历并将每个目标和源分配给下面的列表。我终于将所有数据转换为 JSON 文件。
foreach (var data in dataValue)
{
var value = new RuleJsonclassTemplate
{
type = data.type,
mapping = new List<Mapping>() { new Mapping() { value = data.destination, key = data.source } },
description = data.description,
title = data.title
}
}
string JSONresult = JsonConvert.SerializeObject(value);
string path = outputdir + Outputfilename;
using (var writer = new StreamWriter(path, true))
{
writer.WriteLine(JSONresult.ToString());
writer.Close();
}
class Mapping
{
public string destination { get; set; }
public string source { get; set; }
}
JSON 输出应如下所示,
{
"type": "Type1",
"mapping": [
{
"value": "destination1",
"key": "source1"
},
{
"value": "destination2",
"key": "source1"
},
{
"value": "destination3",
"key": "source3"
}
],
"description": "Test description",
"title": "Test title"
}
您能否建议我如何实现这一目标?供参考我在https://dotnetfiddle.net/W49buW的示例代码
【问题讨论】:
-
@SirRufo:抱歉,更新了所需的 JSON 输出。代码也更新了。
-
@SirRufo:这是我在dotnetfiddle.net/W49buW 添加的一个合适的例子。