0

As a basic example of my situation, lets say I have a class (UserInfo) with two properties. One is Username string, the other named Demographics is a dictionary. The class is marked with DataContract and the props are marked with DataMember. I have an OperationContract in my WCF service that returns my object as JSON. When a request hits and my object is returned it is being serialized as:

{"UserName":"Bobby", "Demographics" : [{"Key": "1", "Value": "Blah1"},{"Key": "2", "Value": "Blah2"}]}

The Demographics is being returned as an array. I want it to be returned as a list. Meaning the bracket should be removed, like this:

EDIT I wrote that object incorrectly

{"Username": "Bobby", "Demographics": { "Key1": { "Value": "Blah1" }, "Key2": { "Value": "Blah2" } } }

My operation contract is:
[OperationContract] [WebInvoke(UriTemplate = "GetNewUserFields", ResponseFormat = WebMessageFormat.Json, Method = "GET"), Description("Get the Fields needed for a new user")] UserInfo GetNewUserFields();

Is there any way to do this? I would post direct code example, however the classes that I am serializing are actually much more complex than what I have described and the return result posted here would be a mess of data to sift through.

Thank you for any help that you may provide.

4
  • 4
    The JSON you want to return is not a valid JSON structure. You can't just nest an object inside an object without a key. Commented Oct 20, 2014 at 14:04
  • You are correct. I am a knucklehead. I edited the above to be correct. Commented Oct 20, 2014 at 14:32
  • 1
    In that case have a look at stackoverflow.com/questions/5124889/… Commented Oct 20, 2014 at 14:46
  • So it looks like I will have to use the JsonConvert to serialize rather than using the built in. Which leads to needing to return a stream. Round about way of getting there, but it works. Thank you much! Commented Oct 20, 2014 at 18:35

0

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.