本文介绍了使用HTTPClient调用WebAPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用httpclient调用web api,我希望能够将JObject绑定到gridview 这里是我的代码,无法正常工作。 />
i am calling a web api using httpclient, i want to be able to bind the JObject to a gridview here is my code that aint working.
protected void Button2_Click(object sender, EventArgs e) { HttpClient client = new HttpClient(); client.BaseAddress = new Uri(&"site name";); // Add an Accept header for JSON format. client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.GetAsync("user/view.json?api_public=M01&api_secret=1;api_name=ezd").Result; if (response.IsSuccessStatusCode) { // Parse the response body. Blocking! string matchIdToFind = "26"; var product = response.Content.ReadAsAsync<JObject>().Result; JObject match = product; GridView1.DataSource = match; GridView1.DataBind(); } else { TextBox3.Text = "errror"; } }这里我得到了所需的响应,但无法将响应绑定到gridview,我们将非常感谢任何帮助或帮助。
Here i have gotten the required response, but could not bind the response to a gridview any help or assistance will be greatly appreciated thanks in advance.
推荐答案您可以使用 github/chris-herring/DataTableConverter [ ^ ]将json转换为datatable像这样: DataTable table = JsonConvert.DeserializeObject< datatable>(json ,新的Serialization.DataTableConverter()); You can use github/chris-herring/DataTableConverter[^] to convert json to datatable Like this: DataTable table = JsonConvert.DeserializeObject<datatable>(json, new Serialization.DataTableConverter());
我发现文章解释 Json .NET - 反序列化DataTable [ ^ ]。 因此,转换为DataTable后,只需将其分配给 GridView1.DataSource 。 I found the article explaining Json .NET–Deserializing a DataTable[^]. So, after converting to DataTable, just assign that to GridView1.DataSource.
更多推荐
使用HTTPClient调用WebAPI
发布评论