sen sen - 4 months ago 14
JSON Question

How to add custom Key, values into json string from C# datatable

my expected output is:

{
key: "user",
values: [
{
"label" : "A" ,
"value" : 29.765957771107
} ,
{
"label" : "B" ,
"value" : 0
} ,
{
"label" : "C" ,
"value" : 32.807804682612
} ,
{
"label" : "D" ,
"value" : 196.45946739256
} ,
{
"label" : "E" ,
"value" : 0.19434030906893
} ,
{
"label" : "F" ,
"value" : 98.079782601442
} ,
{
"label" : "G" ,
"value" : 13.925743130903
} ,
{
"label" : "H" ,
"value" : 5.1387322875705
}
]

}


but what i am getting is

{
"user":[
{
"label":"122",
"value":"387"
},
{
"label":"136",
"value":"402"
},
{
"label":"59T",
"value":"34372"
},
{
"label":"601",
"value":"2970"
},
{
"label":"APA",
"value":"1"
},
{
"label":"B01",
"value":"3376"
},
{
"label":"B11",
"value":"5490"
},
{
"label":"C67",
"value":"5629"
},
{
"label":"CCS",
"value":"1671"
},
{
"label":"F31",
"value":"12"
},
{
"label":"F32",
"value":"4459"
},
{
"label":"FC0",
"value":"18780"
},
{
"label":"FMD",
"value":"15235"
},
{
"label":"GLC",
"value":"3202"
},
{
"label":"GRZ",
"value":"336"
},
{
"label":"HAM",
"value":"651"
},
{
"label":"OP1",
"value":"12771"
},
{
"label":"OP2",
"value":"2065"
},
{
"label":"OP3",
"value":"1519"
},
{
"label":"OZW",
"value":"2866"
},
{
"label":"RAO",
"value":"2907"
},
{
"label":"S00",
"value":"17467"
},
{
"label":"SIN",
"value":"13898"
},
{
"label":"SLE",
"value":"2549"
},
{
"label":"STR",
"value":"542"
},
{
"label":"TCG",
"value":"3628"
},
{
"label":"TCK",
"value":"1487"
},
{
"label":"TCM",
"value":"4729"
},
{
"label":"TCW",
"value":"6348"
},
{
"label":"TSS",
"value":"1543"
},
{
"label":"VA1",
"value":"1139"
},
{
"label":"VA2",
"value":"2370"
},
{
"label":"VA6",
"value":"1556"
},
{
"label":"YC1",
"value":"443"
},
{
"label":"YC2",
"value":"37"
},
{
"label":"YC4",
"value":"1"
},
{
"label":"YC6",
"value":"1"
},
{
"label":"YMW",
"value":"36"
}
]
}


my c# class is below:

public class Propm2345
{
public string label { get; set; }
public string value { get; set; }

}
List<Propm2345> p911 = new List<Propm2345> { };
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
public string get_OPC_Site_view()
{

string sit = "S00";
//select label, value from getOPC_site_count_for_chart
string query = "select label, value from getOPC_site_count_for_chart";
SqlCommand cmd = new SqlCommand(query);
DataSet ds = GetSITData1(cmd);
DataTable dt = ds.Tables[0];

foreach (DataRow item in ds.Tables[0].Rows)
{
Propm2345 pp = new Propm2345();
pp.label = item["label"].ToString();
pp.value = item["value"].ToString();
p911.Add(pp);

}
JavaScriptSerializer jss = new JavaScriptSerializer();
var obj = new { user = p911 };
string ss = jss.Serialize(obj);
return ss;

}


Using the above code i am getting somehow right output but i want to add custom root key and value jsut to feed nvd3 chart.
Please help to achieve the expected json output.

Answer

You have to create object for serialization this way

JavaScriptSerializer jss = new JavaScriptSerializer();
var obj = new { key = "user", values = p911 };
string ss = jss.Serialize(obj);
return ss;