response.content.readasstringasync() to object

ぐ巨炮叔叔 提交于 2021-02-10 14:51:45

问题


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 !!!!


回答1:


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


来源:https://stackoverflow.com/questions/53011610/response-content-readasstringasync-to-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!