我可以知道如何将JSON数据从object转换为datatable吗?我需要在我的tProducts中插入它。以下是我的代码。我从API获得了Json数据。
private async void GetHomeItems()
{
var client = new HttpClient();
var url = new Uri(IAMSUrl + "/GetProductSRP");
var content = new StringContent("{CustomerCode: 'test'}");
var response = await client.PostAsync(url, content);
//As string
var result = await response.Content.ReadAsStringAsync();
//As Object
var objResult = JsonConvert.DeserializeObject<SrpResult>(result);
}
public class SrpResult
{
public int ID { get; set; }
public bool _IsNew { get; set; }
public List<Dictionary<string, dynamic>> Items { get; set; }
}
答案 0 :(得分:1)
第一个解决方案
private async void GetHomeItems()
{
var client = new HttpClient();
var url = new Uri(IAMSUrl + "/GetProductSRP");
var content = new StringContent("{CustomerCode: 'test'}");
var response = await client.PostAsync(url, content);
//As string
var result = await response.Content.ReadAsStringAsync();
//As Object
var objResult = JsonConvert.DeserializeObject<SrpResult>(result);
DataTable dt = (DataTable)JsonConvert.DeserializeObjectt<SrpResult>(objResult.toString(), (typeof(DataTable)));
}
第二个解决方案
https://www.codeproject.com/Tips/1084643/Convert-JSON-To-DataTable-Csharp