none
wcf rest json get object or list<object> RRS feed

  • Question

  • hi, experts,

    [DataContract]
        public class JsonMedia
        {
            [DataMember]
            public string UserID { get; set; }
    
            [DataMember]
            public string Password { get; set; }
    
            [DataMember]
            public string Token { get; set; }
    
            [DataMember]
            public string SessionID { get; set; }
    
            [DataMember]
            public object ReturnObj { get; set; }
        }

    I have create a class that includes some members for authentication (design is for different type of mobile devices , android + ios) , the first 4 members are like token, sessionid, userid , password.

    but the last one is object (or list of object), I would like to set value (by different class object) into ReturnObj  to respond to the request from mobile.

     [WebGet(UriTemplate = "JsonMediaSample", ResponseFormat = WebMessageFormat.Json)]
            public JsonMedia GetJsonMediaSample()
            {
    
                System.Guid key = System.Guid.NewGuid();
                System.Guid sessionID = System.Guid.NewGuid();
                string uid = "delaynomore";
                string password = "xxx123456";
    
                JsonMedia obj = new JsonMedia();
                obj.UserID = uid;
                obj.SessionID = sessionID.ToString();
                obj.Password = password;
                obj.Token = key.ToString();
                Dictionary<int, SampleItem> result = new Dictionary<int, SampleItem>();
    
                SampleItem item1 = new SampleItem(1, "first");
                SampleItem item2 = new SampleItem(2, "second");
                SampleItem item3 = new SampleItem(3, "third");
    
                result.Add(item1.Id, item1);
                result.Add(item2.Id, item2);
                result.Add(item3.Id, item3);
    
                obj.ReturnObj = result;
                return obj;
            }

    however, I found that this approach does not work. is it any further configuration that allow my idea? or totally cannot be done by this approach?

    I would like to have something like below

    {
    "UserID":"stephen",
    "Password":"xxx123456",
    "Token":"16236658-3714-41ca-8c65-66f3ea8ee026",
    "SessionID":"dfa96c41-8c95-4d8b-a1a0-23bc45c1508b",
    "ReturnObj":{
    "1":{
    "Id":1,
    "StringValue":"first"
    },
    "2":{
    "Id":2,
    "StringValue":"second"
    },
    "3":{
    "Id":3,
    "StringValue":"third"
    }
    }
    }

    thank you very much!



    • Edited by delaynomore Tuesday, February 18, 2014 12:48 PM
    Tuesday, February 18, 2014 12:29 PM

All replies

  • List is equivalent to an array, so the value should be represented as a JSON array. And since the body style says that the request needs to be wrapped, then you should wrap the JSON array in an object with a field named as the parameter:

    {"testing":[
        {"A":"Value of A1","B":"Value of B1","C":"Value of C1"},
        {"A":"Value of A2","B":"Value of B2","C":"Value of C2"},
        {"A":"Value of A3","B":"Value of B3","C":"Value of C3"}]}

    If the request were not wrapped (BodyStyle of Bare or WrappedResponse) then you wouldn't need the wrapping object, and this would be a request for the operation:

    [
      {"A":"Value of A1","B":"Value of B1","C":"Value of C1"},
      {"A":"Value of A2","B":"Value of B2","C":"Value of C2"},
      {"A":"Value of A3","B":"Value of B3","C":"Value of C3"}
    ]
    Wednesday, February 19, 2014 10:32 AM