开发者

Help with design of DataContract for my WCF JSON service

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开发者_如何学Python 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 [] ?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜