[英]Convert JSON to Datatable c#
I am trying to convert json string to Datatable in WEBAPI我正在尝试将 json 字符串转换为 WEBAPI 中的数据表
The json string looks like
[
[
"Test123",
"TestHub",
"TestVersion",
"TestMKT",
"TestCAP",
"TestRegion",
"TestAssembly",
"TestProduct",
"Testgroup",
"Testsample",
"1806",
"1807",
"1808",
"1809",
"1810",
"1811",
"1812",
"1901",
"1902",
"1903",
"1904",
"1905",
"1906",
"1907",
"1908",
"1909",
"1910",
"1911",
"1912"
],
[
"Sample12",
"Sample879",
"201806.1.0",
"Sample098",
"TSA CBU",
"B8",
"B8",
"63",
"63EM",
"EM 42 T",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0"
],
[
"Sample121233",
"Sample233879",
"2012323806.1.0",
"Sampl233e098",
"TSA CBU",
"B8",
"B8",
"B3",
"B3ULUE",
"UL 42 R",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0"
]
]
WEBAPI method where i am posting the json i want to convert the json into datatable with some default column names..................................................................................我在其中发布 json 的 WEBAPI 方法我想将 json 转换为具有一些默认列名的数据表 ..................................... ………………………………………………………………………………………………………………………………………………………… ...
public async Task<HttpResponseMessage> Uploadjson(HttpRequestMessage request)
{
try
{
var jsonString = await request.Content.ReadAsStringAsync();
var jArrayObj = JsonConvert.DeserializeObject<JArray>(jsonString);
JArray jsonArray = JArray.Parse(jsonString) as JArray;
DataTable dt = JsonToDataTable(jsonArray[0].ToString());
}
catch (Exception exception)
{
Log.Info("EndPoint Out Time :" + DateTime.Now.ToString());
}
return new HttpResponseMessage(HttpStatusCode.OK);
}
And here is my JsonToDataTable method这是我的 JsonToDataTable 方法
public static DataTable JsonToDataTable(string json)
{
var jsonLinq = JObject.Parse(json);
// Find the first array using Linq
var srcArray = jsonLinq.Descendants().Where(d => d is JArray).First();
var trgArray = new JArray();
foreach (JObject row in srcArray.Children<JObject>())
{
var cleanRow = new JObject();
foreach (JProperty column in row.Properties())
{
// Only include JValue types
if (column.Value is JValue)
{
cleanRow.Add(column.Name, column.Value);
}
}
trgArray.Add(cleanRow);
}
return JsonConvert.DeserializeObject<DataTable>(trgArray.ToString());
//bool columnsCreated = false;
//DataTable dt = new DataTable(tableName);
//Newtonsoft.Json.Linq.JObject root = Newtonsoft.Json.Linq.JObject.Parse(json);
//Newtonsoft.Json.Linq.JArray items = root(tableName);
//Newtonsoft.Json.Linq.JObject item = default(Newtonsoft.Json.Linq.JObject);
//Newtonsoft.Json.Linq.JToken jtoken = default(Newtonsoft.Json.Linq.JToken);
//for (int i = 0; i <= items.Count - 1; i++)
//{
// // Create the columns once
// if (columnsCreated == false)
// {
// item = (Newtonsoft.Json.Linq.JObject) items(i);
// jtoken = item.First;
// while (jtoken != null)
// {
// dt.Columns.Add(new DataColumn(((Newtonsoft.Json.Linq.JProperty)jtoken).Name.ToString()));
// jtoken = jtoken.Next;
// }
// columnsCreated = true;
// }
// // Add each of the columns into a new row then put that new row into the DataTable
// item = (Newtonsoft.Json.Linq.JObject)items(i);
// jtoken = item.First;
// // Create the new row, put the values into the columns then add the row to the DataTable
// DataRow dr = dt.NewRow;
// while (jtoken != null)
// {
// dr(((Newtonsoft.Json.Linq.JProperty)jtoken).Name.ToString()) = ((Newtonsoft.Json.Linq.JProperty)jtoken).Value.ToString();
// jtoken = jtoken.Next;
// }
// dt.Rows.Add(dr);
//}
//return dt;
}
At var jsonLinq = JObject.Parse(json);在 var jsonLinq = JObject.Parse(json); I am getting error我收到错误
Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
It may be too late to answer this OP question.回答这个 OP 问题可能为时已晚。 But this solution may help others in case if they run into similar situation但是,如果其他人遇到类似情况,此解决方案可能会有所帮助
With Cinchoo ETL - an open source library, you can convert this json to DataTable easily使用Cinchoo ETL - 一个开源库,您可以轻松地将此 json 转换为 DataTable
using (var r = ChoJSONReader.LoadText(json))
{
DataTable dt = r.Select(rec => ((object[])rec.Value).ToDictionary()).AsDataTable();
}
If its an valid JSON you can try to use Newtonsoft.Json then you can parse the JSON like this:如果它是一个有效的 JSON,你可以尝试使用 Newtonsoft.Json 然后你可以像这样解析 JSON:
var jsonAsObject = JsonConvert.DeserializeObject(json);
If you have an model for your JSON you can convert it like this:如果你有一个 JSON 模型,你可以像这样转换它:
ModelName model = (ModelName)JsonConvert.DeserializeObject(json, typeof(ModelName));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.