How to put returned Web Service data into Json (GET) for an array or variable in c#

Asked

Viewed 39 times

0

Hi, I’m calling a web service in Rest, I can access the answer but I wanted to organize the data that is returned (which is in Json), for an array or variables but how can I do it? Thank you in advance.

public async Task<string> Get()
        {
            try
            {
                string errorMessage;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                HttpClient client = new HttpClient();
                client.DefaultRequestHeaders.Add("******", "************");
                client.GetType();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));              
                HttpResponseMessage response = await client.GetAsync(EndPoint);
                if (response.IsSuccessStatusCode)
                {
                    var data = await response.Content.ReadAsAsync<ExpandoObject>();
                    var _dataResponse = JToken.Parse(JsonConvert.SerializeObject(data));                       
                    var daraMember = _dataResponse["member"]["href"];                     
                }

                client.Dispose();
          }
          catch(Exception ex)
          {
              errorMessage= ex.Message;
          }

I wanted to get this information:

{"member": [{
"href": "aquamandev.adp.pt/maximo/oslc/os/zwssr/_U1IvMTAwMDYwNQ--"}], 
"href": "aquamandev.adp.pt/maximo/oslc/os/zwssr", 
"responseInfo": 
{ "totalPages": 1, 
  "href": "aquamandev.adp.pt/maximo/oslc/os/…", 
  "totalCount": 1, 
   "pagenum": 1 } 
}
  • you want to consume the data from that webservice?

  • @Amadeuantunes yes and put in an array follows what web service returns in json, I’ll ask the question

1 answer

0

You need to deserialize the _dateResponse for a class that is compatible with this return.

For example:

public class DataResponse
{
    public IList<Member> Members { get; set; }
}

public class Member
{
    [JsonPropertyName("_rowstamp")]
    public string Rowstamp { get; set; }
    
    [JsonPropertyName("url")]
    public string Url { get; set; }
    
    ...
}

EDITION: As I said above, you have to work with serialization/deserialization data.

Serialization is the process of converting the state of an object into a form that can be persisted or transported. The serialization complement is deserialization, which converts a stream into an object. Together, these processes allow data to be stored and transferred.

That process serves for you to convert these streams to classes that your system expects and knows.

Here is an example showing how you would do with your answer. https://dotnetfiddle.net/otZscV

  • please see what I put to the question ? contains the answer of the web service

  • I edited the answer and added an example containing your case. What is worth noting from this is that you need to know beforehand the response of the Web Service, and then you prepare a class with the proper structure to receive the unsecured information.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.