Im trying to format JSON in c# in the following manner. Lets say I have the following table
col1 col2 col3 col4
comA 1 2 3
comB 4 5 6
comC 7 8 9
I would like my JSON Output to be like so
[{
name: 'comA',
data: [1,2,3]
}, {
name: 'comB',
data: [4,5,6]
}, {
name: 'comC',
data: [7,8,9]
}]
I have the following code
public class ChartLoc
{
public string Category { get; set; }
public string Data{ get; set; }
}
public void myFunc(){
using (SqlConnection con = new SqlConnection(ConnectionString)
{
con.Open();
using (SqlCommand cmd = new SqlCommand("select * from table", con))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
List<String> _Category = new List<String>();
List<String> _Data = new List<String>();
while (reader.Read())
{
_Data.Add(reader["col2"].ToString() + ',' + reader["col3"].ToString() + ',' + reader["col4"].ToString());
if (reader["store"] != DBNull.Value) _Category.Add(reader["col1"].ToString());
}
JavaScriptSerializer jss = new JavaScriptSerializer();
cl.Category = jss.Serialize(_Category);
cl.Data = jss.Serialize(_Data);
}
}
}
}
although this output will give me
cl.Category = ['comA','comB','comC'] cl.Data = ['1,2,3','4,5,6','7,8,9']
Related? here is a link from one of them stackoverflow.com/questions/17880171/…