This is my first stack overflow post so cut me some slack :).
I have been struggling quite a while now with this issue.
Currently my WCF reads data from a database and returns it as JSON.
This is how it looks:
{
"shoppinglistitemsResult": [
{
"description": "this is my notes description",
"name": "mynotename",
"pid": "1",
"status": "1",
"username": "test"
}
]
}
I want it to look like this:
{
"shoppinglistitemsResult": [
{
"description": "123",
"name": "123",
"pid": "123",
"status": "123",
"username": "test"
}
],
"success": 1
}
With the extra object at the end.
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "displayAllNotes?name={username}&pass={password}")]
List<Service1.wsNotes> shoppinglistitems(string username, string password);
You would need to return an object that contains both the list and the success property instead of the list directly. Think of every set of curly braces in JSON as a new object/class that needs to be created and everything that is comma separated as properties on that object. So your outer curly braces would need to be represented by a class with two properties (shoppinglistitemsResult and success). You would need a second class for all of the items in your list.
Here’s a way you can do this with generics. I’ve also taken the liberty to include a couple of additional properties you might want to use. I’ve also included a Response type with no “Result” for operations that do not need to return values, but might want to return a success or error message.
And then your operation contract would look something like this…