DataTable嵌套在c#中的json

时间:2014-02-26 06:55:52

标签: c# asp.net json datatable javascriptserializer

我想将DataTable转换为嵌套的JSON。我有一个表公告,其中包含列名称类别,标题,详细信息,Short_desc,作者和Display_date。

Datatabel结果如下:

Category     Headline    Details     Short_desc  Author   Display_date 

Sports       H1          d1          sd1          a1        dd1
Sports       h2          d2          sd2          a2        dd2
Technology   t1          d3          sd3          a3        dd3
Technology   t2          d4          sd4          a4        dd4

现在我希望JSON结果如下:

{   
  "Sports" : [ [ 
        "Headline":"H1",
        "Details":"d1",
        "Short_desc":"sd1",
        "Author":"a1",
        "Display_date":"dd1"
      ],
      [ "Headline":"H2",
        "Details":"d2",
        "Short_desc":"sd2",
        "Author":"a2",
        "Display_date":"dd2"
      ]
    ],
  "Technology" : [ [ 
        "Headline":"t1",
        "Details":"d3",
        "Short_desc":"sd3",
        "Author":"a3",
        "Display_date":"dd3"
      ],
      [ "Headline":"t4",
        "Details":"d4",
        "Short_desc":"sd4",
        "Author":"a4",
        "Display_date":"dd4"
      ]
    ]
}

我使用了以下代码:

DataTable dts = get_banner_detail_service("");

System.Web.Script.Serialization.JavaScriptSerializer serializer = 
    new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in dts.Rows)
{
    row = new Dictionary<string, object>();
    foreach (DataColumn col in dts.Columns)
    {
        row.Add(col.ColumnName, dr[col]);
    }
    rows.Add(row);
}
Response.Write(serializer.Serialize(rows));
Response.Flush();
Response.End();

上述代码的结果与我的预期不符。它就像:

   {   
      "Sports" : [ 
            "Headline":"H1",
            "Details":"d1",
            "Short_desc":"sd1",
            "Author":"a1",
            "Display_date":"dd1"
          ],
       "Sports" :[ 
            "Headline":"H2",
            "Details":"d2",
            "Short_desc":"sd2",
            "Author":"a2",
            "Display_date":"dd2"
          ] ....
        }

2 个答案:

答案 0 :(得分:1)

您需要创建嵌套字典:

        Dictionary<string, Dictionary<string, object>> rows = new Dictionary<string, Dictionary<string, object>>();
        Dictionary<string, object> row;
        foreach (DataRow dr in dts.Rows)
        {
            row = new Dictionary<string, object>();
            var columns = dts.Columns;

            for (int i = 1; i < columns.Count; i++)
            {
                row.Add(columns[i].ColumnName, dr[columns[i]]);
            }

            if (rows.ContainsKey(columns[0].ColumnName))
                rows[columns[0].ColumnName] = rows[columns[0].ColumnName].Concat(row).ToDictionary(p=>p.Key,v=>v.Value);
            else
                rows[columns[0].ColumnName] = row;

        }

答案 1 :(得分:1)

HI最后我解决了这个问题...... 下面是更新的代码 -

DataTable dts = get_banner_detail_service("");
        DataTable dtc = get_banner_detail_cat("");
        List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
        Dictionary<string, object> rowss;
        Dictionary<string, object> rowsc;
        List<object> rowsin;
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        rowsc = new Dictionary<string, object>();
        foreach (DataRow dr in dtc.Rows)
        {
            string cat = dr["Category"].ToString();

            var filteredAndroid = (from n in dts.AsEnumerable()
                                   where n.Field<string>("Category").Contains(cat)
                                   select n).ToList();

            if (filteredAndroid.Count != 0)
            {
                DataTable t = filteredAndroid.CopyToDataTable();

                t.Columns.Remove("Category");
                rowss = new Dictionary<string, object>();

                rowsin = new List<object>();
                foreach (DataRow drr in t.Rows)
                {
                    foreach (DataColumn col in t.Columns)
                    {
                        rowss.Add(col.ColumnName, drr[col]);
                    }
                    rowsin.Add(rowss);
                    rowss = new Dictionary<string, object>();

                }
                rowsc.Add(cat, rowsin);
                t.Dispose();
                t = null;
                filteredAndroid = null;
            }
        }
        rows.Add(rowsc);

        string json = JsonConvert.SerializeObject(rows, Newtonsoft.Json.Formatting.Indented);
        if (json.Length > 2)
        {
            json = json.Substring(1, json.Length - 2);
        }
        Response.Write(json);
        Response.Flush();
        Response.End();