通过调用API URL用数据填充ASP.NET网格视图

5kgi1eie  于 2023-02-14  发布在  .NET
关注(0)|答案(1)|浏览(113)

我的目标是在ASP.NETWebform中通过调用API url来填充一个表。但是会遇到一些序列化问题。
任何帮助将不胜感激。我假设有一些问题与序列化和反序列化。但无法解决。任何帮助将不胜感激。
下面是我的JSON。

"success": true,
    "message": "GOOD",
    "data": [
        {
            "A1": "aa1",
            "A2": "AONE",
            "file_name": "test.pdf",
            "file_type": "application/pdf",
            "created_datetime": "2023-01-10T16:20:01",
            "created_by": "guru",
            "active": true,
            "updated_by": null,
            "updated_datetime": null
        },
        {
            "A1": "aa",
            "A2": "AONE",
            "file_name": "test1.pdf",
            "file_type": "application/pdf",
            "created_datetime": "2023-01-10T16:20:01",
            "created_by": "guru",
            "active": true,
            "updated_by": null,
            "updated_datetime": null
        },
        {
            "A1": "aa1",
            "A2": "AONE",
            "file_name": "test2.pdf",
            "file_type": "application/pdf",
            "created_datetime": "2023-01-10T16:20:01",
            "created_by": "guru",
            "active": true,
            "updated_by": null,
            "updated_datetime": null
        }
    ]
}

下面是我的代码调用API的网址,并显示在网页上的数据。

private async void BindGridView1()
    {
        

        string apiUrl = "http://mesappbeta/BE_API_HOME/api/SeriesBlacklist/Req_UploadedDocs?series=AE01400&series_type=RU";
        using (var client = new HttpClient())
        {
            
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = client.GetAsync(apiUrl).Result;
            if (response.IsSuccessStatusCode)
            {
                
                string json = JsonConvert.SerializeObject(response.Content.ReadAsStringAsync().Result);
                
                
                var data = new List<object> { json };                    
                DataTable dt = new DataTable();
                dt.Columns.Add("file_name");
                dt.Columns.Add("file_type");
                dt.Columns.Add("created_datetime");

                foreach(var item in data)
                {
                    dt.Rows.Add(item);
                }
                GridView2.DataSource = dt;
                GridView2.DataBind();
            }
        }
    }

得到如下输出。

kqhtkvqz

kqhtkvqz1#

试试这个代码

var json = response.Content.ReadAsStringAsync().Result;

    DataTable dt = JObject.Parse(json)["data"].ToObject<DataTable>();

   //  dt.Columns.Remove("A1");  // you can remove columns you don't need

    GridView2.DataSource = dt;
    GridView2.DataBind();

相关问题