[英]Convert JSON to DataTable returns empty result in c#
我正在嘗試將我的 json 數據轉換為數據表。 遵循了這篇文章中的所有方法以及所有建議的鏈接在 stackoverflow 上,嘗試了很多方法,但仍然沒有結果。
我的方法看起來像:
public static async Task GetManagements()
{
Console.WriteLine("Getting managements");
using var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",
await Token.GetToken());
HttpResponseMessage response = await client.GetAsync(LibvirtUrls.managementUrl);
HttpContent content = response.Content;
Console.WriteLine("Response Status Code: " + (int)response.StatusCode);
string result = await content.ReadAsStringAsync();
var json = Helpers.FormatJson(result);
Console.WriteLine("json");
Console.WriteLine(result); // for testing
Console.WriteLine(new string('-',35));
Console.WriteLine("formatted");
Console.WriteLine(json); // for testing
// I used both result and json (formatted and not formatted one)
Console.WriteLine(new string('-',35));
List<NetworkDto> networkList =
JsonConvert.DeserializeObject<List<NetworkDto>>(json);
Console.WriteLine("table view");
var ss = networkList.ToDataTable<NetworkDto>();
Console.WriteLine(ss);
}
示例json是執行方法后:
Getting managements as json
Response Status Code: 200
json
[{"id":1,"netMask":22,"gateway":"10.19.0.1","jenkinsUrl":"http://10.19.0.20:31000","bridge":"br0","ipRange":"10.19.0.1-10.19.3.254"},{"id":11,"netMask":22,"gateway":"10.23.0.1","jenkinsUrl":"http://10.12.3.12:33355","bridge":"br2","ipRange":"10.23.0.4-10.23.0.20"}]
------------------------------------------
formatted
[
{
"id":1,
"netMask":22,
"gateway":"10.19.0.1",
"jenkinsUrl":"http://10.19.0.20:31000",
"bridge":"br0",
"ipRange":"10.19.0.1-10.19.3.254"
},
{
"id":11,
"netMask":22,
"gateway":"10.23.0.1",
"jenkinsUrl":"http://10.12.3.12:33355",
"bridge":"br2",
"ipRange":"10.23.0.4-10.23.0.20"
}
]
------------------------------------------------
table view
也嘗試過類:
public class NetworkDto
{
public int Id { get; set; }
public int NetMask { get; set; }
public string Gateway { get; set; }
public string JenkinsUrl { get; set; }
public string Bridge { get; set; }
public string IpRange { get; set; }
}
我用於將 json 轉換為數據表的擴展方法:
public static DataTable ToDataTable<T>(this IList<T> data)
{
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
for(int i = 0 ; i < props.Count ; i++)
{
PropertyDescriptor prop = props[i];
table.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach (T item in data)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
return table;
}
我檢查了你的代碼:
static void Main() {
string json = "[{\"id\":1,\"netMask\":22,\"gateway\":\"10.19.0.1\",\"jenkinsUrl\":\"http://10.19.0.20:31000\",\"bridge\":\"br0\",\"ipRange\":\"10.19.0.1-10.19.3.254\"},{\"id\":11,\"netMask\":22,\"gateway\":\"10.23.0.1\",\"jenkinsUrl\":\"http://10.12.3.12:33355\",\"bridge\":\"br2\",\"ipRange\":\"10.23.0.4-10.23.0.20\"}]";
System.Data.DataTable dt = (System.Data.DataTable)JsonConvert.DeserializeObject(json, (typeof(System.Data.DataTable)));
Console.WriteLine(dt);
for (int i = 0; i < dt.Columns.Count; i++) {
Console.Write(dt.Columns[i] + " | ");
}
Console.WriteLine();
for (int j = 0; j < dt.Rows.Count; j++) {
for (int i = 0; i < dt.Columns.Count; i++) {
Console.Write(dt.Rows[j].ItemArray[i] + " | ");
}
Console.WriteLine();
}
}
輸出:
身份證 | 網罩 | 網關 | jenkinsUrl | 橋| ip范圍|
1 | 22 | 10.19.0.1 | http://10.19.0.20:31000 | br0 | 10.19.0.1-10.19.3.254 |
11 | 22 | 10.23.0.1 | http://10.12.3.12:33355 | br2 | 10.23.0.4-10.23.0.20 |
看來,數據表被填滿了
從 JSON 反序列化為自定義對象列表很簡單:
List<NetworkDto> networkList = JsonConvert.DeserializeObject<List<NetworkDto>>(json);
就是這樣! 現在您的networkList
變量是一個 NetworkDto 對象列表。 如果您想驗證它們是否在控制台中,您可以將它們全部寫出到控制台:
foreach(var network in networkList)
(
Console.WriteLine($"Network Id {network.Id}");
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.