问题
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