繁体   English   中英

反序列化对象数组:无法将 JSON 值转换为 System.String[]

[英]Deserializing an array of objects: The JSON value could not be converted to 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显示为一个对象数组,而VoskReturnArray模型中的result属性是一个字符串数组。 它应该是这些对象的模型类型数组。 所以而不是:

public string[] result { get; set; }

你会想要这样的:

public ResultItem[] result { get; set; }

...

public class ResultItem
{
    public decimal conf { get; set; }
    public decimal end { get; set; }

    // etc
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM