Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6237621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:03:50+00:00 2026-05-24T11:03:50+00:00

I’m writing a WCF service that returns data in JSON format to clients. I

  • 0

I’m writing a WCF service that returns data in JSON format to clients.
I would like to output JSON which looks like follows

{
22877: {
id: “22877”,
name: “Foo Bar”,
type: “user”,
activities: {
data: [
]
}
}
405564: {
id: “405564”,
name: “Bar Foo”,
type: “user”,
activities: {
data: [
]
}
}
}

I’m having a hard time to come up with a class/DataContract that will be Serialized to the above format by WCF JSON service.

I tried the following

[DataContract]
internal class Person
{
    [DataMember]
    internal string id;

    [DataMember]
    internal string name;

    [DataMember]
    internal string type;

    [DataMember]
    internal Activities activities;
}

[DataContract]
internal class Activities
{
    [DataMember(Name = "data")]
    internal List<Activity> activity;
}

[DataContract]
internal class Activity
{
    internal string name;
}

When I return List of Persons from my WCF method, the JSON response looks as follows

[{“activities”:{“data”:[{}]},”id”:”1234″,”name”:”John”,”type”:”user”},{“activities”:{“data”:[{}]},”id”:”1234″,”name”:”John”,”type”:”user”}]

Any suggestions on how to get “id” (which is dynamic) as a key for each of the inner JSON objects and have {} brackets for a collection of items instead of [] ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:03:51+00:00Added an answer on May 24, 2026 at 11:03 am

    The DataContractJsonSerializer (DCJS) will only be able to serialize (and deserialize) an object graph which follows a defined schema. The data you want to serialize does not do that, so the DCJS cannot be used directly for that.

    One thing you can use, however, is the untyped JSON support which you can get from the codeplex project at http://wcf.codeplex.com (the “WCF Support for jQuery” download). You can find more information at the codeplex site or at http://blogs.msdn.com/b/carlosfigueira/archive/2010/10/29/working-with-untyped-json-in-a-wcf-service.aspx.

    The code below shows how to serialize the data in the format you want; with the new behavior from the codeplex project (WebHttpBehavior3), you can have the JsonObject class be a return type for your operations.

    public class StackOverflow_6990927
    {
        [DataContract]
        internal class Person
        {
            [DataMember]
            internal string id;
            [DataMember]
            internal string name;
            [DataMember]
            internal string type;
            [DataMember]
            internal Activities activities;
        }
        [DataContract]
        internal class Activities
        {
            [DataMember(Name = "data")]
            internal List<Activity> activity;
        }
        [DataContract]
        internal class Activity
        {
            [DataMember]
            public string name;
        }
        public static void Test()
        {
            Activities noActivities = new Activities { activity = new List<Activity>() };
            Activities someActivities = new Activities
            {
                activity = new List<Activity>
                {
                    new Activity { name = "hiking" },
                }
            };
            Person[] people = new Person[]
            {
                new Person { id = "22877", name = "Foo Bar", type = "user", activities = noActivities },
                new Person { id = "405564", name = "Bar Foo", type = "user", activities = someActivities },
            };
            JsonObject jo = new JsonObject(people.Select(x => new KeyValuePair<string, JsonValue>(x.id, JsonValueExtensions.CreateFrom(x))));
            Console.WriteLine(jo);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing WCF service that uses wsHttpBinding binding, which is not hosted in
I'm writing a middle-tier WCF service that needs to fetch some data from a
I'm writing a WCF service, the purpose of which is to authenticate clients using
I have a WCF service that is responsible for writing a log file. I
I'm in the process of writing a WCF service that will allow an ASP.Net
I am writing the client side for a WCF service that supports both synchronous
I'm a bit lost concerning WCF Server authentication... I'm writing a WCF service that
I'm having some trouble writing a secure WCF data service to be consumed by
I am writing a web service using WCF. I created data contracts. I created
I have WCF service that returns an object that contains an array of bytes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.