1

MVCアプリケーションでDatatablesグリッドを使用してデータを表示しています。ジェネリックリストを含むVarをJSONに変換する方法がわかりません。JSONはDatatablesの入力になります。

DatatablesのJSON形式は次のとおりです。

return Json(new 
{
  aaData = new[] {
    new [] { "Trident", "Internet Explorer 4.0", "Win 95+", "4", "X" },
    new [] { "Gecko", "Firefox 1.5", "Win 98+ / OSX.2+", "1.8", "A" },
    new [] { "Webkit", "iPod Touch / iPhone", "iPod", "420.1", "A" },
  }
}, JsonRequestBehavior.AllowGet);

しかし、上記のものはハードコードされたものであり、実行時にジェネリックリストを取得しています。上記のように反復してJSON形式に変更する方法です。

これが私のコードです。

public void GetReport()
        {
            var ReportData = _homeService.GetReportDatafromDB();

            if (ReportData .Count > 0)
            {
               //Dont no how to convert for JSON data format here
            }
       }
4

2 に答える 2

3

foreach 構文を使用すると、次のようになります。

var aaData = new List<string[]>();
foreach(var l in ReportData){
    aaData.Add(new [] {l.Property1, l.Property2, l.Property3, l.Property4});
}

var thisIsYourResult = aaData.ToArray();
于 2013-01-17T07:00:32.123 に答える
1

これは、目的の形式を取得する方法の例です。

演習を簡単にするために、「モデル」をデータ オブジェクト クラスとして使用します。

public class Model{
    public string Property1{get;set;}
    public string Property2{get;set;}
    public string Property3{get;set;}
    public string Property4{get;set;}
}

次に、それを調べて、目的の json 構造を作成するだけです。

var ReportData = new List<Model>{
new Model{Property1 = "Trident", Property2 = "Internet Explorer 4.0", Property3 = "Win 95+", Property4 = "4"},
new Model{Property1 = "Gecko", Property2 = "Firefox 1.5", Property3 = "Win 98+ / OSX.2+", Property4 = "1.8"}
};

var aaData = ReportData.Select(l => new [] {l.Property1, l.Property2, l.Property3, l.Property4} ).ToArray();
于 2013-01-17T06:06:46.097 に答える