我的问题是我如何组合两组JSON,例如一组来自一个数据表,如下所示:
"ID": 1, "SHORT_NAME": "B", "CARRIER_NAME": "Carrier A"
另一个具有多个值的来自另一个数据表:
{ "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);
}
如何使用另一个表的单行添加一个表的多个值。 Doss任何人都知道如何做到这一点。
答案 0 :(得分:1)
要获得json,你的模型应该是这样的:
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; }
}