I am trying to create json structure like below:
{
"cols": [
{
label: "Types",
type: "string"
},
{
label: "values",
type: "number"
}
],
"rows": [
{
c: [
{
v: "Mushrooms"
},
{
v: 3
},
]
},
{
c: [
{
v: "Olives"
},
{
v: 31
}
]
},
{
c: [
{
v: "Zucchini"
},
{
v: 1
},
]
},
{
c: [
{
v: "Pepperoni"
},
{
v: 2
},
]
}
]
}
This above values I am getting from here:
public class Type
{
public int Mushrooms { get; set; }
public int Olives { get; set; }
public int Zucchini { get; set; }
public int Pepperoni { get; set; }
public int Statistics { get; set; } //Ignore properties in final output
public int Count { get; set; } //Ignore properties in final output
public int Average { get; set; } //Ignore properties in final output
}
var types = MyRepository<Type>.FirstorDefault();
The above query outputs:
Mushrooms:3
Olives:31 etc..
This is how I have designed a class:
public class Cols
{
public List<Names> Names { get; set; }
}
public class Names
{
public string label { get; set; }
public string type { get; set; }
}
But I am getting an output like this:
"data": [
{
"Names": [
{
"label": "Types",
"type": "string"
},
{
"label": "values",
"type": "number"
}
]
}
]
Update: Class design
public class Rootobject
{
public List<Names> cols { get; set; }
public List<Row> rows { get; set; }
}
public class Names
{
public string label { get; set; }
public string type { get; set; }
}
public class Row
{
public List<C> c { get; set; }
}
public class C
{
public object v { get; set; }
}
Still above class design gives me below json structure:
{
"Data": {
"cols": [
{
"label": "Types",
"type": "string"
},
{
"label": "values",
"type": "number"
}
],
"rows": [
{
"c": [
{
"v": "Mushrooms"
},
{
"v": "3"
},
{
"v": "Olives"
},
{
"v": "31"
},
{
"v": "Zucchini"
},
{
"v": "1"
},
{
"v": "Pepperoni"
},
{
"v": "2"
}
]
}
]
}
}
For Returning json data I am using this json class of asp.net mvc.
return Json(new {Data = rootObject }, JsonRequestBehavior.AllowGet);