我需要创建以下格式的JSON文件:
"Result": [
[ "Date", "Name", "Address", "Age" ],
[ "MMDDYYYY", "Name1", "Add1", "15" ],
[ "MMDDYYYY", "Name2", "Add2", "20" ]
结果的数据是由DataTable
填充的List<string>
填充的List<JArray>
,如下所示:
var jArrayList = new List<JArray>();
var rowData = new List<string>();
for (int i = 0; i < table.Rows.Count; i++)
{
var rowData = new List<string>(table.Columns.Count);
DataRow dataRow = table.Rows[i];
foreach (DataColumn dataColumn in table.Columns)
{
if (i > 0)
{
rowData.Add(dataRow[dataColumn].ToString());
}
}
if (rowData.Any())
{
jArrayList.Add(new JArray(rowData));
}
}
if (jArrayList.Any())
{
var jobj = new JObject( new JProperty("Result",
from p in jArrayList
select p));
jObjList.Add(jobj);
}
var outputJson = new JObject(new JProperty("Entries",
from p in jObjList
select p));
var outputString = outputJson.ToString();
当我执行这段代码时,它将所有内容写入新行:
"Result": [
[
"Date",
"Name",
"Address",
"Age"
],
[
"MMDDYYYY",
"Name1",
"Add1",
"15"
],
[
"MMDDYYYY",
"Name2",
"Add2",
"20"
],
如何格式化以这种方式编写?
"Result": [
[ "Date", "Name", "Address", "Age" ],
[ "MMDDYYYY", "Name1", "Add1", "15" ],
[ "MMDDYYYY", "Name2", "Add2", "20" ]
此处:
string json = ""Result": [rn [rn "Date",rn "Name",rn "Address",rn "Age"rn ],rn [rn "MMDDYYYY",rn "Name1",rn "Add1",rn "15"rn ],rn [rn "MMDDYYYY",rn "Name2",rn "Add2",rn "20"rn ],";
var withoutEnter = json.Replace(Environment.NewLine, "");
var splited = withoutEnter.Split('[');
var formattedJson = splited[0];
for (int i = 1; i < splited.Length; i++)
{
formattedJson = formattedJson + "[" + splited[i] + "n";
}
我已经复制粘贴了你的json样本,并解析它以删除所有的输入,然后为每个组添加回输入。抱歉,代码有点乱:)