2014-03-07 4 views
0
내 문제는 내가 한 세트는 다음과 같이 하나의 데이터 테이블에서 예를 들어 JSON 두 세트를 온다 결합 할 수있는 방법입니다

JSON은

{ "YMDH": "2009-03-07 00:00:00.000", "SELL_DURATION": 222.999995 }, { "YMDH": "2009-03-07 01:00:00.000", "SELL_DURATION": 75.816664 }, { "YMDH": "2009-03-07 02:00:00.000", "SELL_DURATION": 39.349995 }, { "YMDH": "2009-03-07 03:00:00.000", "SELL_DURATION": 75.816664 }, { "YMDH": "2009-03-07 04:00:00.000", "SELL_DURATION": 39.349995 }, { "YMDH": "2009-03-07 05:00:00.000", "SELL_DURATION": 275.91666 } 


Since first table will have only one records and another table will be having multiple records for that one row. how can i combine them both in my controller class to build proper JSON like this: 

var nwCustomers = [{ "ID": 1, "SHORT_NAME": "A", "CARRIER_NAME": "Carrier A", "SellDuration": [{ "YMDH": "2009-03-07 00:00:00.000", "SELL_DURATION": 222.999995 }, { "YMDH": "2009-03-07 01:00:00.000", "SELL_DURATION": 75.816664 }, { "YMDH": "2009-03-07 02:00:00.000", "SELL_DURATION": 39.349995 }, { "YMDH": "2009-03-07 03:00:00.000", "SELL_DURATION": 75.816664 }, { "YMDH": "2009-03-07 04:00:00.000", "SELL_DURATION": 39.349995 }, { "YMDH": "2009-03-07 05:00:00.000", "SELL_DURATION": 275.91666 }] }  
    ]; 

I'm tring to do something like this but not able to get the desired output: 

모델 :

public class GridModel 
{ 
    public DateTime YMDH { get; set; } 
    public double ID { get; set; } 
    public string SHORT_NAME { get; set; } 
    public string CARRIER_NAME { get; set; } 
    public double SELL_DURATION { get; set; }  
    public GridSparklineModel SellDuration { get; set; } 
} 


public class GridSparklineModel 
{ 
    public DateTime YMDH { get; set; } 
    public double SELL_DURATION { get; set; }  
} 

Controller: 


public ActionResult FetchGraphDataJSON() 
     { 
      Grid grid = new Grid(); 
      DataSet ds = grid.GetHistoryData(); 

      DataTable dt = ds.Tables[0]; 

      List<GridModel> data = new List<GridModel>(); 

      if (dt.Rows.Count != 0) 
      { 
       StringBuilder sb = new StringBuilder();     

       foreach (DataRow row in dt.Rows) 
       { 
        sb.Append(new GridModel { ID = Convert.ToDouble(@row["ID"].ToString()), SHORT_NAME = @row["SHORT_NAME"].ToString() }); 

        double carrierId = Convert.ToDouble(@row["ID"].ToString()); 

        DataRow[] rowsInOtherTable = ds.Tables[1].Select("ID = " + carrierId); 

        for(int i=0; i < rowsInOtherTable.Count(); i++) 
        { 
         // add with existing 
        } 

        data.Add(new GridModel { ID = Convert.ToDouble(@row["ID"].ToString()), SHORT_NAME = @row["SHORT_NAME"].ToString(), CARRIER_NAME = @row["CARRIER_NAME"].ToString(), SellDuration = new GridSparklineModel { YMDH = DateTime.Parse(@rowsInOtherTable[0]["YMDH"].ToString()), SELL_DURATION = Convert.ToDouble(@rowsInOtherTable[0]["SELL_DURATION"].ToString()) } }); 
       } 
      } 

      return Json(data, JsonRequestBehavior.AllowGet); 
     } 

이 어떻게 다른 테이블의 단일 행에 하나 개의 테이블의 여러 값을 추가 할 수 있습니다. 누구든지이 일을하는 법을 알았어.

public class GridModel 
{ 
    public int ID { get; set; } 
    public string SHORT_NAME { get; set; } 
    public string CARRIER_NAME { get; set; } 
    public List<SellDuration> SellDuration { get; set; } 
} 

public class SellDuration 
{ 
    public DateTime YMDH { get; set; } 
    public double SELL_DURATION { get; set; } 
} 
+0

당신이 개체 목록에서 JSON을 구축하려고 : – Muctadir

답변

1

이 모델은 다음과 같이해야한다는 JSON을 얻으려면?