【发布时间】:2019-03-22 02:37:15
【问题描述】:
我有一个JSON 字符串,其中记录可以更改。我在我的 JSON 字符串中的每个级别都使用public partial classes。我将它复制到DataTable,因为我在我的 SQL-Server 中需要它。我这样调用 JSON:
StringReader sr = new StringReader(json);
Newtonsoft.Json.JsonTextReader readera = new JsonTextReader(sr);
object result = (Welcome)jsona.Deserialize(readera,typeof(Welcome));
Welcome w = (Welcome)result;
DataTable da = w.Result.Records.ToDataTable();
例如,我的JSON 可以包含:
ID、名称、值
但其他JSON 可能包含:
身份证、地址、城市、邮政编码
我的班级现在看起来像这样:
public partial class Record
{
[JsonProperty("Col1")]
public DateTimeOffset Col1 { get; set; }
[JsonProperty("Col2")]
public long Col2 { get; set; }
[JsonProperty("Col3")]
public DateTimeOffset Col3 { get; set; }
[JsonProperty("Col4")]
public long Col4 { get; set; }
[JsonProperty("Col5")]
public string Col5 { get; set; }
如何使这个动态化?我真的不知道。
【问题讨论】:
标签: c# json sql-server class dynamic