Deserializing JSON from Web Service

Kmcnet 786 Reputation points
2024-08-21T19:53:32.1633333+00:00

Hello everyone and thanks for the help in advance. I have a Windows application that posts json to a webservice which in turn returns a json response. I am using a class that looks like:

    public class DeserializedClass
    {
        public class Message
        {
            [JsonProperty("@xmlns:xsi")]
            public string XmlnsXsi { get; set; }
            [JsonProperty("@DatatypesVersion")]
            public string DatatypesVersion { get; set; }
            [JsonProperty("@TransportVersion")]
            public string TransportVersion { get; set; }
            [JsonProperty("@TransactionDomain")]
            public string TransactionDomain { get; set; }
            [JsonProperty("@TransactionVersion")]
            public string TransactionVersion { get; set; }
            [JsonProperty("@StructuresVersion")]
            public string StructuresVersion { get; set; }
            [JsonProperty("@ECLVersion")]
            public string ECLVersion { get; set; }
            public Header Header { get; set; }
            public Body Body { get; set; }
        }
 
        public class Root
        {
            public Message Message { get; set; }
        }
    }

I sent the message:

            DeserializedClass deserializedClass = new DeserializedClass();

            DeserializedClass.Root messageroot = new DeserializedClass.Root();

            messageroot.Message = new DeserializedClass.Message

The application returns json in the format:

{"Message":{  etc.

The call to the web service looks like string resp = await ServiceSoapClient.Api(myjson);

However, I cannot figure out how to deserialize the response. Any help would be appreciated.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,838 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 64,006 Reputation points
    2024-08-21T22:13:21.5866667+00:00

    assuming ServiceSoapClient.Api returns the json string as described its:

    string resp = await ServiceSoapClient.Api(myjson);
    DeserializedClass.Root? root = JsonSerializer.Deserialize<DeserializedClass.Root>(resp);
    

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 64,006 Reputation points
    2024-08-21T20:35:11.85+00:00

    a sample of the response json would help


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.