簡體   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