{ "result":true, "code":200, "data":{ "count":6, "success":true, "table":[ { "Availability":100, "Cloud":1, "Col":1182, "DataSourceID":"L1A0003869534", "Date":2019030724, "ID":"1105250", "Level":8, "Provider":"", "Row":518, "Satellite":"GF1", "Sensor":"WFV1", "Size":"", "TileFileName":"GF1_WFV1_2019030724_L1A0003869534_6401_8_518_1182", "Time":"", "type":"Preview", "types":"1,2,3,4" }, { "Availability":100, "Cloud":0, "Col":1182, "DataSourceID":"L1A0006618962", "Date":2022072324, "ID":"1105293", "Level":8, "Provider":"", "Row":518, "Satellite":"GF1", "Sensor":"WFV1", "Size":"", "TileFileName":"GF1_WFV1_2022072324_L1A0006618962_6400_8_518_1182", "Time":"", "type":"Preview", "types":"1,2,3,4" } ] } }如何用C#转换为datatable
时间: 2023-07-03 12:26:42 浏览: 354
云计算Hadoop:快速部署Hadoop集群
你可以使用 Newtonsoft.Json 库来将 JSON 数据转换成 DataTable,以下是示例代码:
```csharp
using Newtonsoft.Json;
using System.Data;
public DataTable JsonToDataTable(string json)
{
DataTable dataTable = new DataTable();
JObject jsonObject = JObject.Parse(json);
JArray tableArray = JArray.Parse(jsonObject["data"]["table"].ToString());
foreach (JToken item in tableArray)
{
if (dataTable.Columns.Count == 0)
{
foreach (JProperty property in item)
{
dataTable.Columns.Add(property.Name, typeof(string));
}
}
DataRow dataRow = dataTable.NewRow();
foreach (JProperty property in item)
{
dataRow[property.Name] = property.Value.ToString();
}
dataTable.Rows.Add(dataRow);
}
return dataTable;
}
```
调用方式:
```csharp
string json = "{\"result\":true,\"code\":200,\"data\":{\"count\":6,\"success\":true,\"table\":[{\"Availability\":100,\"Cloud\":1,\"Col\":1182,\"DataSourceID\":\"L1A0003869534\",\"Date\":2019030724,\"ID\":\"1105250\",\"Level\":8,\"Provider\":\"\",\"Row\":518,\"Satellite\":\"GF1\",\"Sensor\":\"WFV1\",\"Size\":\"\",\"TileFileName\":\"GF1_WFV1_2019030724_L1A0003869534_6401_8_518_1182\",\"Time\":\"\",\"type\":\"Preview\",\"types\":\"1,2,3,4\"},{\"Availability\":100,\"Cloud\":0,\"Col\":1182,\"DataSourceID\":\"L1A0006618962\",\"Date\":2022072324,\"ID\":\"1105293\",\"Level\":8,\"Provider\":\"\",\"Row\":518,\"Satellite\":\"GF1\",\"Sensor\":\"WFV1\",\"Size\":\"\",\"TileFileName\":\"GF1_WFV1_2022072324_L1A0006618962_6400_8_518_1182\",\"Time\":\"\",\"type\":\"Preview\",\"types\":\"1,2,3,4\"}]}}";
DataTable dataTable = JsonToDataTable(json);
```
阅读全文