Reputation: 33
I'm struggling with this function i've been searching for awhile but for some reason it can't get the outcome i desire. I have a stored procedure which returns the following data. Query Results Img
Here is the class i'm using for the list:
[DataContract]
public class EvtTypes
{
[DataMember]
public string EvtType { get; set; }
[DataMember]
public int EvtCnt { get; set; }
}
Here is the code the makes the call:
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "getEvtTypesCnt")]
List<EvtTypes> GetEvtTypesCnt();
public List<EvtTypes> GetEvtTypesCnt()
{
var data = new StHomeDBDataContext();
var list = new List<EvtTypes>();
foreach (var r in data.GetAllEvtTypesCnt_ToList())
{
list.Add(new EvtTypes
{
EvtType = r.evtType,
EvtCnt = Convert.ToInt32(r.evtCnt)
});
}
return list;
}
which when called returns this:
[
{
"EvtCnt": 18,
"EvtType": "alarmSystemStatus"
},
{
"EvtCnt": 13871,
"EvtType": "battery"
},
{
"EvtCnt": 210,
"EvtType": "button"
},
{
"EvtCnt": 23,
"EvtType": "color"
},
{
"EvtCnt": 3777,
"EvtType": "contact"
},
{
"EvtCnt": 88,
"EvtType": "door"
},
{
"EvtCnt": 103440,
"EvtType": "energy"
},
{
"EvtCnt": 304,
"EvtType": "heatingSetpoint"
},
{
"EvtCnt": 24,
"EvtType": "hue"
}
]
I really want to format it to look like this:
{
"evtType": [{
"name": "name here",
"cnt": 2323
}]
}
Upvotes: 2
Views: 97
Reputation: 11463
It sounds like this is a serialization question. You can customize the names and order the properties using the DataContract/DataMember attributes:
[DataContract]
public class EvtTypes
{
[DataMember(Name="cnt", Order=1)]
public string EvtType { get; set; }
[DataMember(Name="name", Order=2)]
public int EvtCnt { get; set; }
}
By default, JSON serialization uses the name of your property as the field name in the JSON object. You can override that using the Name
property of DataMember
. Also, the default sort order for field names seems to be alphabetic order. You can override that by specifying the Order
property on DataMember
.
JQuery doesn't care about the order of the field names of your JSON object and you can access them directly. You may choose not to worry about field order.
To get the rest of the JSON structure you want, just keep in mind that the {}
notation is a map. You can get that by encapsulating it like this:
Dictionary<string,List<EvtType>> map = new Dictionary<string,List<EvtType>>();
map.Add("evtType", list);
Don't forget to change the return type for you method.
The full replacement for your web service would be:
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "getEvtTypesCnt")]
Dictionary<string,EvtTypes> GetEvtTypesCnt();
public Dictionary<string,EvtTypes> GetEvtTypesCnt()
{
var data = new StHomeDBDataContext();
var list = new List<EvtTypes>();
foreach (var r in data.GetAllEvtTypesCnt_ToList())
{
list.Add(new EvtTypes
{
EvtType = r.evtType,
EvtCnt = Convert.ToInt32(r.evtCnt)
});
}
Dictionary<string,List<EvtType>> map =
new Dictionary<string,List<EvtType>>();
map.Add("evtType", list);
return map;
}
If I understood your intent properly.
Upvotes: 1
Reputation: 145
You would need object of following structure:
public class EvtType
{
public string Name { get; set; }
public int Cnt { get; set; }
}
public class RootObject
{
public List<EvtType> EvtType { get; set; }
}
Upvotes: 1