none
Use IList with JSON in WCF service RRS feed

  • Question

  • Hi!

    I trying to create service for next json:

    {
      "FirstId": "id1",
      "SecondId": 3,
      "Mainid": "id0",
      "event": "eventId",
      "resource": {
        "id": 5,
        "fields": {
          "Custom.Field1": "Value1",
          "Custom.Field2": "Value2",
          "Custom.FieldN": "Value2"
           }
       }
    }

    I created next service:

    [OperationContract]
            [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare,
                RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
                UriTemplate = "MyFunction")]
            string MyFunction(Content Event);

    An I created data contract:

    [DataContract]
        public class Content
        {
            [DataMember]
            public String FirstId { get; set; }
            [DataMember]
            public int SecondId{ get; set; }
            [DataMember]
            public String Mainid { get; set; }
            [DataMember]
            public String event { get; set; }
            [DataMember]
            public Resource resource { get; set; }
        }
    
        [DataContract]
        public class Resource
        {
            [DataMember]
            public int id { get; set; }
            [DataMember]
            public IList<FieldInfo> fields { get; set; }
        }
    
        [DataContract]
        public class FieldInfo
        {
            [DataMember]
            public String name { get; set; }
            [DataMember]
            public String value { get; set; }
        }

    All work fine except the "fields" member. it always has lenght 0.

    How can I convert the "fields" member  to List with pair Name/Value?

    • Moved by Kristin Xie Tuesday, February 2, 2016 4:46 AM WCF related
    Monday, February 1, 2016 10:54 AM

Answers

  • Your JSON does not match the data contract, hence why it is not coming across correctly. In order for the serializer to match an IList<T> member it expects an enumerable list of items. For JSON that is an array.  Your fields member in JSON isn't an array, it is a custom object with members called custom.Field1, etc.  That cannot be converted to an IList object.

    My JSON is a little rusty but I believe this is more what you want for the JSON.

    {
      "FirstId": "id1",
      "SecondId": 3,
      "Mainid": "id0",
      "event": "eventId",
      "resource": {
        "id": 5,
        "fields": [
          {
            "name": "Custom.Field1",
            "value": "Value1"
          },
          {
            "name": "Custom.Field2",
            "value": "Value2"
          },
          {
            "name": "Custom.FieldN",
            "value": "Value2"
          }
        ]
      }
    }

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Monday, February 1, 2016 3:48 PM
    Moderator

All replies

  • First off, you are posting to the wrong forum.

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wcf

    Secondly, you should just send or receive the Json data string, and you then convert the Json into a concreate class and load it into a List<T> on the client or service side.

    There are plenty of articles using Bing or Google about converting Json into .NET objects. 

    All the stuff about IList,  a Datacontract and Json is suspect and is most likely not how it's done that I know about, seen or is it going to work is questionable.

    Monday, February 1, 2016 12:20 PM
  • Your JSON does not match the data contract, hence why it is not coming across correctly. In order for the serializer to match an IList<T> member it expects an enumerable list of items. For JSON that is an array.  Your fields member in JSON isn't an array, it is a custom object with members called custom.Field1, etc.  That cannot be converted to an IList object.

    My JSON is a little rusty but I believe this is more what you want for the JSON.

    {
      "FirstId": "id1",
      "SecondId": 3,
      "Mainid": "id0",
      "event": "eventId",
      "resource": {
        "id": 5,
        "fields": [
          {
            "name": "Custom.Field1",
            "value": "Value1"
          },
          {
            "name": "Custom.Field2",
            "value": "Value2"
          },
          {
            "name": "Custom.FieldN",
            "value": "Value2"
          }
        ]
      }
    }

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Monday, February 1, 2016 3:48 PM
    Moderator
  • Yes, you right )
    Tuesday, February 2, 2016 9:31 AM