c# - 反序列化对象数组:无法将 JSON 值转换为 System.String[]
问题描述
无法使用 JSON.Net 访问数组内的对象
public class VoskReturnArray
{
public string[] result { get; set; }
public string text { get; set; }
}
var voskArray = JsonSerializer.Deserialize<VoskReturnArray>(rec.Result());
Console.WriteLine(voskArray.text); // Works fine
Console.WriteLine(voskArray.result); // Throws an exception
试过了<List<VoskReturnArray>>
,但text
不会出现。
我在这里想念什么?
数据:
{
"result" : [{
"conf" : 0.228337,
"end" : 0.141508,
"start" : 0.060000,
"word" : "one"
}, {
"conf" : 1.000000,
"end" : 0.390000,
"start" : 0.141508,
"word" : "hundred"
}, {
"conf" : 1.000000,
"end" : 1.080000,
"start" : 0.390000,
"word" : "employees"
}],
"text" : "one hundred employees that's where it is you know they had the same problems are those five employees companies but they have the money to pay to fix them"
}
解决方案
您的数据显示result
为对象数组,而模型中的result
属性VoskReturnArray
是字符串数组。它应该是这些对象的模型类型的数组。所以而不是:
public string[] result { get; set; }
你会想要这样的东西:
public ResultItem[] result { get; set; }
...
public class ResultItem
{
public decimal conf { get; set; }
public decimal end { get; set; }
// etc
}
推荐阅读
- excel - 如何为 Win32 API 声明变量?
- scala - 在 Scala spark 中使用任何数据类型
- javascript - 如何使用moment.js获取从当前月份到过去一个月的所有月份列表
- javascript - 我如何编写一个脚本来不断重新加载并向端点发出请求,直到添加特定项目
- reactjs - 将 material-ui 类转换为函数
- python - python中文本分类的过采样?
- html - “rvest”未使用 html_nodes() 获取产品详细信息
- javascript - 如何使用命令在 discord.js 中获取服务器 ID 和频道 ID?
- binary - 二进制格式和位格式
- ssl - 从 HTTP 重定向到 HTTPS 后 curl 无法验证自签名证书