我有以下格式的 json 字符串
{
"open": [
3978,3856,3925,3918,3877.85,3992.7,4033.95,4012,3910,3807,3840,3769.5,3731,3646,3749,
3770,3827.9,3851,3815.3,3791
],
"high": [
3978,3925,3929,3923,3977,4043,4041.7,4012,3920,3851.55,3849.65,3809.4,3733.4,3729.8,
3758,3808,3864,3882.5,3824.7,3831.8
],
"low": [
3861,3856,3836.55,3857,3860.05,3962.3,3980,3910.5,3811,3771.1,3740.1,3722.2,3625.1,
3646,3721.4,3736.4,3800.65,3816.05,3769,3756.15
],
"close": [
3879.85,3915.9,3859.9,3897.9,3968.15,4019.15,3990.6,3914.65,3826.55,3833.5,3771.35,
3769.9,3649.25,3690.05,3736.25,3800.65,3856.2,3824.6,3814.9,3779
],
"volume":[
3937092,1906106,3203744,6684507,3348123,3442604,2389041,3102539,6176776,3112358,
3258414,3330501,5718297,3143862,2739393,2105169,1984212,1960538,2307366,1919149
],
"start_Time": [
1326220200,1326306600,1326393000,1326479400,1326565800,1326825000,1326911400,
1326997800,1327084200,1327170600,1327429800,1327516200,1327689000,1327775400,
1328034600,1328121000,1328207400,1328293800,1328380200,1328639400
]
}
我还安装了 Nuget packege Newtonsoft.json
我尝试了代码
var unwrappedJson = JsonConvert.DeserializeObject<string>(result);
DataTable dataTable = (DataTable)JsonConvert.DeserializeObject(unwrappedJson, (typeof(DataTable)));
但出现错误
正如评论中提到的,您的 JSON 结构是一个对象,其中每个字段都是一个值数组,不支持反序列化为
DataTable
。
您需要将对象数据expand/transpose转换为对象数组(抱歉,我不确定哪个英文术语适合描述)。
[
{ "high": /* value */, "low": /* value */, ... }
]
对于实施总结,您需要:
获取对象中带有字段的列名称。
根据字段数组中值的数量获取行数(假设所有字段的数量相同)。
获取列数。
迭代每一行和每一列(索引),通过索引获取各自的值,形成每行的值数组。
将数组添加到
DataRow.ItemArray
中,并将DataRow
添加到DataTable
中。
public DataTable ExpandObjectToDataTable(JObject jObj)
{
DataTable dt = new DataTable();
if (jObj == null)
return null;
List<string> columns = jObj.Properties()
.Select(x => x.Name)
.ToList();
// Add columns
dt.Columns.AddRange(columns.Select(x => new DataColumn(x)).ToArray());
var arrayValues = jObj.Values();
// Get number of rows by calculating the number of values in the field
int totalRow = arrayValues.Select(x => x.Count()).Max();
int totalColumn = columns.Count;
// Iterate row
for (int i = 0; i < totalRow; i++)
{
DataRow row = dt.NewRow();
List<object> values = new List<object>();
// Iterate field
for (int j = 0; j < totalColumn; j++)
{
values.Add(arrayValues.ElementAt(j).ElementAt(i));
}
row.ItemArray = values.ToArray();
dt.Rows.Add(row);
}
return dt;
}
来电者:
JObject jObj = JObject.Parse(json);
DataTable dt = ExpandObjectToDataTable(jObj);
好吧,当您能够实现上述概念时,您可以考虑构建一个 Custom Json Converter (可选)。