PeeBee PeeBee - 1 year ago 96
JSON Question

C# items as root nodes of JSON

I'm trying to pass a Json that I'd like to access from jquery as,


jdata.comType


my c# code is,

var frontChartList = new List<object>();
frontChartList.Add(new
{
comType = comType,
today = DateTime.Now.ToString("D"),
agentsAdded = "53",
agentsAvail = "47",
packageAvailDays = leftDays.ToString(),

});
JavaScriptSerializer jss = new JavaScriptSerializer();
String json = jss.Serialize(frontChartList);
return json;


I cannot access this as


jdata.comType
only as,
jdata[0].comType


how should I create the JSON to get a string accessible as jdata.comType?
since I will only be passing one object in this.

zzT zzT
Answer Source

Because your frontChartList is a List<object>, change it to single object instead:

        var frontChartList = new
        {
            comType = comType,
            today = DateTime.Now.ToString("D"),
            agentsAdded = "53",
            agentsAvail = "47",
            packageAvailDays = leftDays.ToString(),

        });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download