我想创建一个JSON文件并对其进行描述。第一张照片展示了它的外观。它应该看起来像第二张图片。因此我的编程代码是错误的。有人有解决方案吗?因为当我启动我的代码所以它总是只写一个,其他东西他总是覆盖。如何使它像第二个(带标题(dxfFiles,vipFile)和结构)?< / p>
感谢您的回答
JObject j = null;
foreach (Detection v in listVIP)
{
j = new JObject(
new JProperty("Dateiname", v.m_sFilename),
new JProperty("Objekt-ID", v.m_sObjectID),
new JProperty("Unterordner", v.m_sSubfolder),
new JProperty("Url-Download", v.m_sDownloadlink)
);
foreach (Detection d in listDXF)
{
j = new JObject(
new JProperty("Dateiname", d.m_sFilename),
new JProperty("Objekt-ID", d.m_sObjectID),
new JProperty("Unterordner", d.m_sSubfolder),
new JProperty("Url-Download", d.m_sDownloadlink)
);
j.Add("Dateiname", "");
}
}
File.WriteAllText(
(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\test.json"), j.ToString()
);
答案 0 :(得分:0)
您可以像这样创建所需的JSON:
JArray jaVIP = new JArray();
foreach (Detection v in listVIP)
{
jaVIP.Add(new JObject(
new JProperty("Dateiname", v.m_sFilename),
new JProperty("Objekt-ID", v.m_sObjectID),
new JProperty("Unterordner", v.m_sSubfolder),
new JProperty("Url-Download", v.m_sDownloadlink)));
}
JArray jaDFX = new JArray();
foreach (Detection d in listDXF)
{
jaDFX.Add(new JObject(
new JProperty("Dateiname", d.m_sFilename),
new JProperty("Objekt-ID", d.m_sObjectID),
new JProperty("Unterordner", d.m_sSubfolder),
new JProperty("Url-Download", d.m_sDownloadlink)));
}
JObject j = new JObject(
new JProperty("dxfFiles", jaDFX),
new JProperty("vipFile", jaVIP));
File.WriteAllText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) +
"\\test.json", j.ToString());