JSON to c# class serialization returns null

387 Views Asked by At

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.

1

There are 1 best solutions below

0
Ding Peng On

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.