【问题标题】:Retrieve data table from JSON response in ASP.NET or C# code?从 ASP.NET 或 C# 代码中的 JSON 响应中检索数据表?
【发布时间】:2018-01-31 07:26:07
【问题描述】:

从 API 请求中获取 JSON 响应,如下所示。我想从这个 JSON 响应创建一个数据表。如何使用 C# .net 应用程序从 JSON 响应中检索数据表?

 {
    "status": "success",
    "data": [
        [
            {
                "value": "Department of Information Technology and Communication",
                "key": "POSTED_DEPARTMENT"
            },
            {
                "value": 5800002,
                "key": "APPOINTING_DEPT_ID"
            },
            {
                "value": 170,
                "key": "EMP_CADRE"
            },
            {
                "value": "Department of Information Technology and Communication",
                "key": "APPOINTING_DEPT"
            },
            {
                "value": "RJBI198009003722",
                "key": "UNIQUE_ID"
            },
            {
                "value": 5800002,
                "key": "POSTED_DEPARTMENT_ID"
            },
            {
                "value": 3004216,
                "key": "EMP_ID"
            },
            {
                "value": null,
                "key": "DATE_OF_BIRTH"
            },
            {
                "value": "06 Jan 2018",
                "key": "IPR_SUBMITTED_DATE"
            },
            {
                "value": "Programmer",
                "key": "DESIGNATION"
            },
            {
                "value": null,
                "key": "PROPERTY_YEAR"
            },
            {
                "value": "DINESHARORA",
                "key": "EMPLOYEE_NAME"
            },
            {
                "value": "Mr.MURLI MANOHAR ARORA",
                "key": "FATHER_NAME"
            },
]
],
 "msg": "Data Successfully Retrived"
}

【问题讨论】:

  • 你可以使用Json.Net
  • 我正在使用 Json.net,但数据格式不正确。感谢您的即时回复
  • 显示你有什么代码,不要让我们猜测。
  • 显示你的尝试。

标签: c# asp.net json


【解决方案1】:

我想这是将复杂的 JSON 响应转换为 DataTable 的一种正确方法。试试下面的。创建用于反序列化 JSON 数据的类

 public class Example
{
    public string status { get; set; }
    public IList<IList<Datum>> data { get; set; }
    public string msg { get; set; }
}

public class Datum
{
    public object value { get; set; }
    public string key { get; set; }
}

我选择示例 JSON 格式并尝试使用方法上的代码

string filter1 =  "{\"status\": \"success\", \"data\": [[ {\"value\": \"RJBI198009003722\",  \"key\": \"UNIQUE_ID\"},  {\"value\": 5800002, \"key\": \"POSTED_DEPARTMENT_ID\"},],[{\"value\": \"Department of Information Technology and Communication\", \"key\": \"POSTED_DEPARTMENT\" }] ],\"msg\":\"Data Successfully Retrived\"}";

        var dat = JsonConvert.DeserializeObject<Example>(filter1);
        var result = new List<Datum>();
        foreach (var item in dat.data)
        {
            for (int i = 0; i < item.Count; i++)
            {
                result.Add(item[i]);
            }

        }
        DataTable table = new DataTable();
        table = ConvertToDataTable<Datum>(result);

以下是可以将任意列表对象转换为数据表的方法。

public static DataTable ConvertToDataTable<T>(IList<T> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(T));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (T item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }

数据表格式如下图。

谢谢!

【讨论】:

  • 谢谢,它救了我的工作,如果我只想显示特定的行,那怎么可能呢?
  • Sanjeev 我想用值而不是默认键和值绑定列名
  • 终于可以绑定数据了
猜你喜欢
  • 2020-06-18
  • 2020-03-12
  • 2017-08-10
  • 1970-01-01
  • 2013-07-28
  • 1970-01-01
  • 2019-01-12
  • 1970-01-01
  • 2014-01-03
相关资源
最近更新 更多