如何将对象转换为列表 [英] How to convert object to list
本文介绍了如何将对象转换为列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨这是我获取数据如何转换为Json结果后的列表或DataTable。
我尝试了什么: < br $>
Hi This is I Get Data how to i convert to list or DataTable Following Json Result.
What I have tried:
{"AuditPrm": [
{
"AuditId": 1,
"AuditId": 7,
"Remarks": "Test Cond",
"UserId": 4
},
{
"AuditId": 2,
"AuditId": 7,
"Remarks": "Test Cond",
"UserId": 4
}
]}
dynamic dynObj = JsonConvert.DeserializeObject(obj.ToString());
<pre> Audit empObj = JsonConvert.DeserializeObject<Audit>(dynObj);
推荐答案
您可以使用json2csharp.com将您的json转换为对象模型
- 转到json2csharp.com
- 在框中过去你的JSON。
- 点击Generate。
- 您将获得对象模型的C#代码
- 反序列化
You can use json2csharp.com to Convert your json to an object model
- Go to json2csharp.com
- Past your JSON in the Box.
- Click on Generate.
- You will get C# Code for your object model
- Deserialize by
using NewtonJson;
...
var model = JsonConvert.DeserializeObject
public static DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
//Get all the properties
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
//Setting column names as Property names
dataTable.Columns.Add(prop.Name);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
//inserting property values to datatable rows
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
//put a breakpoint here and check datatable
return dataTable;
}
这篇关于如何将对象转换为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文