Musaab Zeeshan Musaab Zeeshan - 2 years ago 59
C# Question

Customized output from wcf service

I want to get the output from my WCF Service as

While i am getting output as

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