user1254053
user1254053

Reputation: 765

JSON in controller class

My question is how can I combine two sets of JSON for example one set comes from one datatable like this:

"ID": 1, "SHORT_NAME": "B", "CARRIER_NAME": "Carrier A"

Another one with multiple values comes from another datatable:

{ "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:

Model:

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);
        }

How can I add multiple values of one table with single row of another table. Doss anybody know how to do this.

Upvotes: 0

Views: 98

Answers (1)

EZI
EZI

Reputation: 15354

To get that json your model should be like this:

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; }
}

Upvotes: 1

Related Questions