1

My problem is that I cannot extract an object sent by an API, despite I have the schema of the object either in the API or in the client. My code:

 public async Task<ActionResult> Index()
    {
        HttpClient client = new HttpClient();
        
        
        Uri baseAddress = new Uri("http://localhost:44237/");
        client.BaseAddress = baseAddress;

       
        HttpResponseMessage response = client.GetAsync("api/Front/Subm?IdSubmission=1xxx").Result;

        try
        {
            if (response.IsSuccessStatusCode)
            {
                
                
                string Result = await response.Content.ReadAsStringAsync();
                JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
                Submission sub = JsonConvert.DeserializeObject<Submission>(Result);

                return View(sub);
            }
            else
            {
                
            }
        }
        catch (Exception e)
        {
            
        }
    }

structure received in result is

enter image description here

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
jalil monagi
  • 39
  • 1
  • 2
  • 9
  • First of all its an `async` method so use await instead of `.Result`. `HttpResponseMessage response = await client.GetAsync("api/Front/Subm?IdSubmission=1xxx");` Second, check if the string you get is the correct object – Ihusaan Ahmed Oct 26 '18 at 19:40

1 Answers1

0

You didn't use the instance JavaScriptSerializer which is created in your code:

JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
Submission sub = JsonConvert.DeserializeObject<Submission>(Result);

Try to modify your code like this:

string Result = await response.Content.ReadAsStringAsync();
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
Submission sub = jsonSerializer.DeserializeObject(Result);
Grace Feng
  • 16,564
  • 2
  • 22
  • 45