简体   繁体   中英

Deserializing an array of objects: The JSON value could not be converted to System.String[]

Can't access objects inside the array using 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

Tried with <List<VoskReturnArray>> , but then text won't show up.

What am I missing here?

Data:

{
  "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"
}

Your data shows result as an array of objects, while your result property in VoskReturnArray model is an array of string. It should be instead an array of a model type for those objects. So rather than:

public string[] result { get; set; }

You'd want something like:

public ResultItem[] result { get; set; }

...

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

    // etc
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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