使用第三方API数据源绑定数据网格

本文关键字:数据 数据网 网格 绑定 数据源 第三方 API | 更新日期: 2023-09-27 18:03:58

我使用C# 4.5。我已经创建了一个windows应用程序。

根据项目要求,我需要绑定一个第三方api响应,该响应返回json对象数组到datagrid

我使用RestClient获取数据,并使用JDynamic包将它们转换为对象

IRestResponse response = client.Execute(request);                 
dynamic result = new JDynamic(response.Content);

是否有任何技术,我可以绑定这个数组的对象作为gridview的数据源?

另外,如果我只需要显示某些列该怎么办?

编辑:


JSON响应类似于:

[
    {
        "id": 7,
        "username": "pthakur@abc.com",
        "first_name": "Carlos",
        "middle_name": "",
        "last_name": "Alaiz",
        "gender": "male",
        "addr1": "",
        "addr2": "",
        "state": "",
        "city": "",
        "country": "",
        "post_code": "",
        "email_id": "pthakur@abcd.com",
        "email_id_alt": "",
        "phone1": "1800777333",
        "phone2": "",
        "photo": "2015-09-09-17-14-29-2015-01-22-06-26-15-400-don-passport-lighting-test 044-3-100pc_pp-ps.jpg",
        "populations": [{
            "id": 8,
            "population_name": "Pop 8"
        }]
    },
    {
        "id": 8,
        "username": "shailesh.lakum@abc.com",
        "first_name": "Pedro",
        "middle_name": "",
        "last_name": "Montero",
        "gender": "male",
        "addr1": "",
        "addr2": "",
        "state": "",
        "city": "",
        "country": "",
        "post_code": "",
        "email_id": "pedro@spam4.me",
        "email_id_alt": "",
        "phone1": "1800999222",
        "phone2": "",
        "photo": "2015-09-09-17-13-40-2014-10-27-06-59-56-bilde.jpeg",
        "populations": [{
            "id": 8,
            "population_name": "Pop 8"
        }]
    },
    {
        "id": 9,
        "username": "mario@abcd.com",
        "first_name": "Mario",
        "middle_name": "",
        "last_name": "Sanz",
        "gender": "male",
        "addr1": "",
        "addr2": "",
        "state": "",
        "city": "",
        "country": "",
        "post_code": "",
        "email_id": "mario@xyz.com",
        "email_id_alt": "",
        "phone1": "1800288299",
        "phone2": "",
        "photo": "2015-09-09-17-13-17-2014-10-27-06-28-30-Jim-Smith.jpg",
        "populations": [{
            "id": 7,
            "population_name": "Pop 7"
        }]
    },
    {
        "id": 10,
        "username": "pthakur@xyz.com",
        "first_name": "Parikshit",
        "middle_name": "",
        "last_name": "Thakur",
        "gender": "male",
        "addr1": "",
        "addr2": "",
        "state": "",
        "city": "",
        "country": "",
        "post_code": "",
        "email_id": "pthakur1@xyz.com",
        "email_id_alt": "",
        "phone1": "1800299200",
        "phone2": "",
        "photo": "2015-09-10-11-52-05-parikshit.jpg",
        "populations": [{
            "id": 8,
            "population_name": "Pop 8"
        }]
    },
    {
        "id": 11,
        "username": "nidhi.patel1234@xyz.com",
        "first_name": "Nidhi ",
        "middle_name": "",
        "last_name": "Patel",
        "gender": "female",
        "addr1": "",
        "addr2": "",
        "state": "",
        "city": "",
        "country": "",
        "post_code": "",
        "email_id": "nidhi@spam4.me",
        "email_id_alt": "",
        "phone1": "1800200300",
        "phone2": "",
        "photo": "female.jpg",
        "populations": []
    }
]
我已经为它创建了一个新类,如下所示:
namespace DemoAutoLogin
{
    public class patient
    {
        public int id { get; set; }
        public string username { get; set; }
        public string first_name { get; set; }
        public string middle_name { get; set; }
        public string last_name { get; set; }
        public string gender { get; set; }
        public string addr1 { get; set; }
        public string addr2 { get; set; }
        public string state { get; set; }
        public string city { get; set; }
        public string country { get; set; }
        public string post_code { get; set; }
        public string email_id { get; set; }
        public string email_id_alt { get; set; }
        public string phone1 { get; set; }
        public string phone2 { get; set; }
        public string photo { get; set; }
        public List<Population> populations { get; set; }
    }
    public class Population
    {
        public int id { get; set; }
        public string population_name { get; set; }
    }
}

现在,我该怎么做才能在网格视图中显示JSON记录?

使用第三方API数据源绑定数据网格

然后您可以简单地从结果反序列化到IEnumerable<patient>。使用Json。NET来做这个。IEnumerable<patient>你可以绑定到网格。

public void BindMyData()
{
    IEnumerable<patient> patients = JsonConvert.DeserializeObject<IEnumerable<patient>>(resultAsJson);
    datagrid.DataSource = patients;
}

应该是这样的

如果没有poco:

public void BindMyData()
{
    IEnumerable<Dictionary<string, object>> patients = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(resultAsString);
    // if this works, i'm not sure:
    datagrid.DataSource = patients;
}

如果你不能将IEnumerable<Dictionary<string, object>>绑定到网格中,你也许可以绑定一个DataTable。添加ToDataTable扩展:

public static class Exts
{
    public static DataTable ToDataTable(this IEnumerable<Dictionary<string, object>> list)
    {
        DataTable result = new DataTable();
        if (!list.Any())
            return result;
        var columnNames = list.SelectMany(dict => dict.Keys).Distinct();
        result.Columns.AddRange(columnNames.Select(c => new DataColumn(c)).ToArray());
        foreach (var item in list)
        {
            var row = result.NewRow();
            foreach (var key in item.Keys)
                row[key] = item[key];
            result.Rows.Add(row);
        }
        return result;
    }
}

,然后这样绑定:

public void BindMyData()
{
    IEnumerable<Dictionary<string, object>> patients = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(resultAsString);
    datagrid.DataSource = patients.ToDataTable();
}