繁体   English   中英

从 json 输入 C# 创建动态 class 和 data.table

[英]Create dynamic class and data table from json input in C#

是否可以从 JSON 输入文件创建动态 C# class。 我正在尝试重写我的代码,因为我们将不得不从不同的供应商导入大量 JSON 文件,并希望使代码可重复和可重用。

请在下面找到 JSON 示例。

JSON1:

[{
"date":1647820800,
"humidity":75.72,
"pressure":1018.67,
"windspeed":3.88
},
{
"date":1647907200,
"humidity":75.35,
"pressure":1018.73,
"windspeed":4.47
}
]

C# 第一类:

public class Weather
{
    public int date { get; set; }
    public double humidity { get; set; }
    public double pressure { get; set; }
    public double windspeed { get; set; }
}

JSON2:

[{
      "Id": 1,
      "productname": "item1",
      "supplier": "abc inc",
      "quantity": 500,
      "unitcost": "$12.37"
    }, 
    {
      "Id": 2,
      "productname": "Mountain Juniperus ashei",
      "supplier": "def. co.",
      "quantity": 100,
      "unitcost": "$9.78"
    }
]

C# Class2:

public class Supplier
{
    public int Id { get; set; }
    public string productname { get; set; }
    public string supplier { get; set; }
    public int quantity { get; set; }
    public string unitcost { get; set; }
}

我正在使用 Nwtonsoft JSON 反序列化数据。

string jsonFile = "JSON1.json";
dynamic json = JsonConvert.DeserializeObject<ExpandoObject>(jsonFile, new ExpandoObjectConverter());

错误:

Unable to cast object of type 'System.Collections.Generic.List'[System.Object]' to type 'System.Dynamic.ExpandoObject'

此外,无论使用哪个 JSON 文件,都尝试动态使用DataTable

数据表:

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("date", typeof(int)));
dt.Columns.Add(new DataColumn("humidity", typeof(double)));
dt.Columns.Add(new DataColumn("pressure", typeof(double)));
dt.Columns.Add(new DataColumn("windspeed", typeof(double)));

DataRow dr = dt.NewRow();

for (int i = 0; i < json.weather.Count; i++)
{
    {
        dr = dt.NewRow();
        dr["date"] = json.weather[i].date;
        dr["humidity"] = json.weather[i].humidity;
        dr["pressure"] = json.weather[i].pressure;
        dr["windspeed"] = json.weather[i].windspeed;
        dt.Rows.Add(dr);
    }
}

您不需要任何类来创建 DataTable

var json= @"[{
""date"":1647820800,
""humidity"":75.72,
""pressure"":1018.67,
""windspeed"":3.88
},
{
""date"":1647907200,
""humidity"":75.35,
""pressure"":1018.73,
""windspeed"":4.47
}
]"; 

DataTable datatable=JsonConvert.DeserializeObject<DataTable>(json);

这是供应商名单

List<Supplier> suppliers =JsonConvert.DeserializeObject<List<Supplier>>(json);

class

public partial class Supplier
{
    [JsonProperty("Id")]
    public long Id { get; set; }

    [JsonProperty("productname")]
    public string Productname { get; set; }

    [JsonProperty("supplier")]
    public string SupplierName { get; set; }

    [JsonProperty("quantity")]
    public long Quantity { get; set; }

    [JsonProperty("unitcost")]
    public string Unitcost { get; set; }
}

如果您不喜欢创建 class 来反序列化每个 json 响应,您应该尝试使用Newtonsoft.Json.Linq中的JObject类型,如下所示

using Newtonsoft.Json.Linq;

string content = await response.Content.ReadAsStringAsync();

JObject resp = JObject.Parse(content);

Console.WriteLine(resp);
Console.WriteLine(resp["id"]);
Console.WriteLine(resp["name"]);

您的响应是 json 个对象的List ,因此您应该像下面那样反序列化它,而不是像您那样得到转换类型错误。

var suppliers = JsonConvert.DeserializeObject<List<Supplier>>respo.response);
//JObject resp = JObject.Parse(content);

foreach (var supplier in suppliers)
    Console.WriteLine(supplier["productname "]);

]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM