Musaab Zeeshan Musaab Zeeshan - 1 year ago 39
C# Question

Customized output from wcf service

I want to get the output from my WCF Service as
{"taskList":[{"oid":"30774","Id":"4122"}]}

While i am getting output as
[{"Key":"taskList","Value":[{"oid":"30774","Id":"4122"}]}]

My code is as



public Dictionary<string, List<JobModel>> getList(Job jobList)
{
Dictionary<string, List<JobModel>> dataDict = new Dictionary<string, List<JobModel>>();
dataDict.Add("taskList", jobList);
string str = JsonConvert.SerializeObject(dataDict.Values);
return dataDict;
}





Any idea how can i achieve the goal.

Answer Source

You don't need to create a dictionary, simply add jobList to a new anonymous object with a property called taskList as the root element (assuming jobList is really a List<JobModel>):

string str = JsonConvert.SerializeObject(new { taskList = jobList });

Which will output {"taskList":[{"oid":"30774","Id":"4122"}]}.

Obviously you also want your WCF service to return this string value, not the dictionary itself.

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