Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
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.
C#
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";
               }



       }


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.
Posted
Updated 10-Dec-13 5:21am
v2
Comments
Can't you cast JObject to DataTable?
Uwakpeter 10-Dec-13 12:52pm    
No, any assistance will be appreciated!
Please check my answer.
Uwakpeter 11-Dec-13 8:20am    
I have checked your answer, trying to implement it but I am having this error: jOject is a variable but it is being used as a method, pls any suggestion on how to fix this will be appreciated and also, how will I call the method on my button click event and supplied the corresponding argument to the parameters? Thanks in advance.
You have to show me the code where this issue comes, otherwise I can't visualize.

I found the article explaining Json .NET–Deserializing a DataTable[^].

So, after converting to DataTable, just assign that to GridView1.DataSource.
 
Share this answer
 
You can use https://github.com/chris-herring/DataTableConverter[^] to convert json to datatable Like this:
DataTable table = JsonConvert.DeserializeObject<datatable>(json, new Serialization.DataTableConverter());
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900