1

I have a webservice, which I want to POST JSON data. I want to serialize the submitted data into a class and some of the serialized data always returns null.

Here is the problematic part of the json:

"contacts": {
        ..."1": {
            "name": "tesz1",
            "phone": "3600000000",
            "email": null,
            "title": null,
            "try_before_reach" : 0
        },
         "2": {
            "name": "tesz1",
            "phone": "3600000000",
            "email": null,
            "title": null,
            "try_before_reach" : 0
        }
    }...

And this is the class :

....
    [DataMember(Name = "contacts")]
    public Dictionary<string, Contact> contacts { get; set; } 

}

[DataContract]
public class Contact
{
    [DataMember(Name = "name")]
    public string name { get; set; }

    [DataMember(Name = "phone")]
    public string phone { get; set; }

    [DataMember(Name = "email")]
    public string email { get; set; }

    [DataMember(Name = "title")]
    public string title { get; set; }
    [DataMember(Name = "try_before_reach")]
    public int try_before_reach { get; set; }
}

The webservice:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "/VCC/", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    Task<WebhookResponse> ResponseAsync(VCCModel request);

}

And the request.Contact returns with null everytime.

Anyone have any idea what the problem might be?

Thank you very much.

dbc
  • 104,963
  • 20
  • 228
  • 340
  • By "webservice" you mean a [tag:wcf] web service? Or something else? Please specify the exact framework and version you are using, as different versions use different JSON serializers. – dbc Jul 30 '20 at 14:50
  • your class structure looks good. this should be deserialize. How are calling service and code for serialization and deserialization. Please share. – m.k.sharma Jul 30 '20 at 14:52
  • Because if you are really using [tag:wcf], then note that the `DataContractJsonSerializer` used by [tag:wcf] serializes dictionaries as key/value arrays not as objects, see [Collections, Dictionaries and Arrays](https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/stand-alone-json-serialization#collections-dictionaries-and-arrays). While it's possible to modify this behavior for standalone serialization, there's no way to pass the necessary setting into WCF. – dbc Jul 30 '20 at 15:04
  • Yes, i'm using wcf. Can you suggest me a solution how can i serialize this with DataContractJsonSerializer, or is it possible to replace somehow the serializer? – Attila Lazúr Jul 30 '20 at 22:11
  • Can you change the JSON instead? It's much much easier to just format your JSON as required by `DataContractJsonSerializer`. – dbc Aug 02 '20 at 20:57
  • Hi, is the problem resolved? If the problem persists, please feel free to let me know. – Ding Peng Aug 10 '20 at 07:20

1 Answers1

0

According to the information you provided, I did a test but did not encounter the situation you mentioned. I suspect that there may be an error in your request format or your "request.Contact" is Null.Here is my demo:

Class

[DataContract]
public class Contact
{
    [DataMember(Name = "name")]
    public string name { get; set; }

    [DataMember(Name = "phone")]
    public string phone { get; set; }

    [DataMember(Name = "email")]
    public string email { get; set; }

    [DataMember(Name = "title")]
    public string title { get; set; }
    [DataMember(Name = "try_before_reach")]
    public int try_before_reach { get; set; }
}
[DataContract]
public class Respose {
    [DataMember(Name = "contacts")]
    public Dictionary<string, Contact> contacts { get; set; }
}

webservice

        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/VCC/", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Task<Respose> ResponseAsync(string Test);

Implementation class

public Task<Respose> ResponseAsync(string Test)
        {
            Respose respose = new Respose();
            Contact contact1 = new Contact();
            contact1.email = "e1";
            contact1.name = "n1";
            contact1.phone = "p1";
            contact1.title = "t1";
            contact1.try_before_reach = 0;

            Contact contact2 = new Contact();
            contact1.email = "e1";
            contact1.name = "n1";
            contact1.phone = "p1";
            contact1.title = "t1";
            contact1.try_before_reach = 0;
            Dictionary<string, Contact> dict = new Dictionary<string, Contact>() { { "Test1",contact1 }, { "Test2", contact1 } };
            respose.contacts = dict;
            return Task.FromResult<Respose>(respose);
        }

The picture below is the information I got in Postman:

enter image description here

1.You need to check if your request is correct.

2.Check if your "request.Contact" is null.

Feel free to let me know if the problem persists.

Ding Peng
  • 3,702
  • 1
  • 5
  • 8