0

Below are my two model classes

public class ResponseData
{
  public List<string> labels { get; set; }
  public List<string> series { get; set; }
  public List<dataForMetric>  data{ get; set; }        
}

public class dataForMetric
{
   public List<int> value { get; set; }
}

and my action method is

 public ActionResult GetData()
 { 
   ResponseData res = new ResponseData();
   res.labels = new List<string>() { "day1", "day2", "day3" , "day4"};
   res.series = new List<string>() { "dummy" };
   res.data = new List<dataForMetric>() { new dataForMetric() { value = new List<int>() {10} } ,
                                               new dataForMetric() { value = new List<int>() {110} } ,
                                               new dataForMetric() { value = new List<int>() {120} } ,
                                               new dataForMetric() { value = new List<int>() {130} } 
                                             };
        return  Json(res, JsonRequestBehavior.AllowGet);           
    }

The JSON output of above action method is

{"labels":["day1","day2","day3","day4"],"series":["dummy"],"data":[{"value":[10]},{"value":[110]},{"value":[120]},{"value":[130]}]}

But for my requirement the output should be

{"labels":["day1","day2","day3","day4"],"series":["dummy"],"data":[[10],[110],[120],[130]]}

Please let me know how it can be achieved.

3 Answers 3

1

Well if you actually want output like you describe you should chenge your class to this:

public class ResponseData
{
    public List<string> labels { get; set; }
    public List<string> series { get; set; }
    public int[][] data { get; set; }
}

I generate this class with VS 2013. It now has feature to create class structure from JSON. Edit -> Paste Special -> Pase JSON as classes. Hope this instrument will ease your life a lot.

Sign up to request clarification or add additional context in comments.

Comments

0

Your dataForMetric class is unnecessary. You can achieve the desired result with a list of lists:

public class ResponseData
{
    public List<string> labels { get; set; }
    public List<string> series { get; set; }
    public List<List<int>> data { get; set; }        
}

public ActionResult GetData()
{ 
    ResponseData res = new ResponseData();
    res.labels = new List<string> { "day1", "day2", "day3" , "day4"};
    res.series = new List<string> { "dummy" };
    res.data = new List<List<int>> {
        new List<int> { 10 },
        new List<int> { 110 },
        new List<int> { 120 },
        new List<int> { 130 }
    };
    return  Json(res, JsonRequestBehavior.AllowGet);           
}

Comments

0

You must either define data as a single dataForMetric or define dataForMetric.value as a single int.

Your problem is you're instantiating a List of a List of int.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.