简体   繁体   中英

response.content.readasstringasync() to object

Hy Guys !!

My problem is that i can not 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

but my object is always : enter image description here

Thank s for your Help !!!!

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM