1

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);

2 Answers 2

1

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.

[DataContract]
public class Response : IExtensibleDataObject
{
    public Response()
    {
        Success = true;
        ErrorMessage = null;
    }

    [DataMember]
    public bool Success { get; set; }
    [DataMember]
    public string ErrorMessage { get; set; }

    public ExtensionDataObject ExtensionData { get; set; }
}

[DataContract]
public class Response<TResult> : Response
{
    [DataMember]
    public TResult Result { get; set; }
}

And then your operation contract would look something like this...

[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "displayAllNotes?name={username}&pass={password}")]
Response<List<Notes>> GetShoppingListItems();
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for you answer. I am really having trouble returning 2 objects to one class. I'm really not good when it comes to c# and wcf. If I have an 2 arrays. How would I return them both in a class?
If it would make it easier for you, you could write up what you think the JSON would look like and then back into your result class from there.
If you need to return two lists, you would create a class that has two properties on it (one for each list). And then you would return an instance of that class. If you need further clarification, just reply so and I can write an example.
0

Then build a class that contains as members a list and the extra object you need and return an instance of that class as json.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.