scorpio1441
scorpio1441

Reputation: 3098

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

Upvotes: 0

Views: 105

Answers (1)

Noah Stahl
Noah Stahl

Reputation: 7593

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
}

Upvotes: 2

Related Questions