如何在C#Windows应用程序中删除并将所需的json数据绑定到datagridview?

时间:2017-02-06 12:42:28

标签: c# json datagridview

Json数据:

{
"template": {
    "section": "Total",
    "totalRecievedTillDate": null,
    "receivedFiles": "0",
    "attendedFiles": "0",
    "pendingFiles": "0",
    "totalActionPendingTillDate": null,
    "totalActionCompletedTappal": null,
    "last15DaysActionCompleted": null,
    "last15DaysRecievedTappal": null
},
"sectiontappals": [
    {
        "section": "Collectorate-A Section",
        "totalRecievedTillDate": null,
        "receivedFiles": "0",
        "attendedFiles": "0",
        "pendingFiles": "0",
        "totalActionPendingTillDate": null,
        "totalActionCompletedTappal": null,
        "last15DaysActionCompleted": null,
        "last15DaysRecievedTappal": null
    },
    {
        "section": "Collectorate-B Section",
        "totalRecievedTillDate": null,
        "receivedFiles": "0",
        "attendedFiles": "0",
        "pendingFiles": "0",
        "totalActionPendingTillDate": null,
        "totalActionCompletedTappal": null,
        "last15DaysActionCompleted": null,
        "last15DaysRecievedTappal": null

我想删除第一个字段(模板),并希望将来自sectiontappals的数据绑定到C#中的datagridview。有可能吗?

我试过了:

using (var client = new HttpClient()) {
    string path = "uri";
    using (var response = await client.GetAsync(path)) {
        if (response.IsSuccessStatusCode) {
            var data = await response.Content.ReadAsStringAsync();
            dashbordgrd.DataSource = JsonConvert.DeserializeObject<List<listclass>>(data);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

创建下面的类,仅仅为了示例,我在模板类中只创建了一个属性。

public class template
    {
        public string section{get; set;}
        private List<sectiontappal> _sectionappal;
        public List<sectiontappal> sectiontappals
        {
            get
            {
                if (_sectionappal == null)
                    _sectionappal = new List<sectiontappal>();
                return _sectionappal;
            }
            set
            {
                _sectionappal = value;
            }
        }
    }

将NewtonJson添加到您的项目&amp;使用以下代码来实现您的要求:

System.IO.StreamReader reader = new System.IO.StreamReader(AppDomain.CurrentDomain.BaseDirectory + "/1.txt");
string result = reader.ReadToEnd();
var data = JsonConvert.DeserializeObject<template>(result);
dataGridView1.DataSource = data.sectiontappals;

我已将你的json放在名为1.txt的文件中。

由于