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

亚历克斯·G

无法使用 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
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 Newtonsoft.Json 反序列化 C# 对象时出错:将值“xxx”转换为类型“System.String[]”时出错

将 JSON 转换为对象失败 - 无法将当前 JSON 对象反序列化为 System.Collections.Generic.List

RestSharp反序列化List <object>返回无法强制转换或从System.String转换为

无法反序列化当前JSON对象..以键入'System.Collections.Generic.List`1 [System.String]'

无法反序列化对象。无法将类型java.util.Date的值转换为String

无法反序列化对象。无法将 java.lang.String 类型的值转换为 long

获取错误:无法反序列化对象。无法将 java.lang.String 类型的值转换为 long

System.Text.Json - JSON 值无法转换为 System.String

System.Text.Json对象数组反序列化

尝试调用控制器端点时,“无法将JSON值转换为System.String”

无法将获取 JSON 值的 POST 请求转换为 System.String。路径错误

Newtonsoft.Json.JsonSerializationException:转换值时出错,无法将System.String强制转换或转换为DashBoard.DashboardReport

将JSON转换为对象会引发JsonMappingException“无法从START_ARRAY令牌中反序列化类的实例”

Powershell 5:无法将值转换为System.String

ASP.NET Core 3.0 [FromBody]字符串内容返回“无法将JSON值转换为System.String。”

使用System.Text.Json反序列化数组json对象的嵌套数组

使用Unity JsonUtility反序列化JSON数组无法获取序列化的值

反序列化JSON对象时,是否可以将属性名称转换为小写?

如何将 json 值转换为小写 - 反序列化

使用JSON.stringify的“数组反序列化不支持Type System.String”错误

W/System.err: org.json.JSONException: 类型 java.lang.String 的值可能无法转换为 JSONArray

如何将键/值对String转换为JSON对象?

JSON值无法转换为System.DateTime

如何使用System.Text.Json API将流反序列化到对象

如何反序列化JSON对象,但将特定字段保留为String而不是嵌套对象?

System.InvalidCastException:'无法将类型为'System.String'的对象转换为类型为'Newtonsoft.Json.Linq.JToken'。”

序列化:无法将类型string [] []转换为string []

无法处理参数“机器”上的参数转换。无法将值转换为 System.String 类型

如何将Newton.Json反序列化的通用对象转换为自定义对象?