我需要以JSON格式获取查询结果,但如果列是JSON数组,我会遇到问题:它会转换为字符串
我当前使用的代码如下:
var result = new List<Dictionary<string, object>>();
await using (var cmd = new NpgsqlCommand("SELECT * FROM global", conn))
await using (var reader = await cmd.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
var row = new Dictionary<string, object>();
for (int i = 0; i < reader.FieldCount; i++)
{
row[reader.GetName(i)] = reader.GetValue(i);
}
result.Add(row);
}
}
var jsonResult = JsonConvert.SerializeObject(result, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine(jsonResult);
输出:
[
{
"id": 1,
"banned": false,
"strikes": "[]"
},
{
"id": 2,
"banned": false,
"strikes": "[\"nooob\"]"
}
]
例外输出:
[
{
"id": 1,
"banned": false,
"strikes": []
},
{
"id": 2,
"banned": false,
"strikes": ["nooob"]
}
]