C#,不知道如何使用这个 Json 反序列化
总而言之,我正在尝试将语音转文本调用的结果传递给 IBM 的 Watson,并将最终转录本身转换为纯文本。转录的输出最终会是这种格式的一团 Json,
{
"result_index": 0,
"results": [
{
"final": true,
"alternatives": [
{
"transcript": "%HESITATION %HESITATION %HESITATION %HESITATION %HESITATION it is probably is actual words okay what's my best ",
"confidence": 0.74
}
]
}
]
}
当通过 JSON 到 C# 转换器运行时,会导致类似这样的结果
public class Alternative
{
public string transcript { get; set; }
public double confidence { get; set; }
}
public class Result
{
public bool final { get; set; }
public List<Alternative> alternatives { get; set; }
}
public class Root
{
public int result_index { get; set; }
public List<Result> results { get; set; }
}
最终,我只想在替代下获取该转录并将其发送到文本框,但是我一生都无法做到这一点,这就是我所能得到的,不知道从这里到哪里去得到那个成绩单。
var bAudio = File.ReadAllBytes(file);
var memAudio = new MemoryStream(bAudio);
var results1 = speechToText.Recognize(memAudio, model: "en-US_NarrowbandModel");
Root resultsTranscription = JsonConvert.DeserializeObject<Root>(results1.Response);
我最终得到 0 输出。我似乎无法准确理解这是如何将 JSON 分解为不同的组件
Long and short, I'm trying to take the results from a speech to text call to IBM's Watson, and get the final transcription itself into plain text. The output of the transcription ends up being a mess of Json in this format,
{
"result_index": 0,
"results": [
{
"final": true,
"alternatives": [
{
"transcript": "%HESITATION %HESITATION %HESITATION %HESITATION %HESITATION it is probably is actual words okay what's my best ",
"confidence": 0.74
}
]
}
]
}
which when run through a JSON to C# Converter, leads to something like this
public class Alternative
{
public string transcript { get; set; }
public double confidence { get; set; }
}
public class Result
{
public bool final { get; set; }
public List<Alternative> alternatives { get; set; }
}
public class Root
{
public int result_index { get; set; }
public List<Result> results { get; set; }
}
Ultimately, I just want to get to that transcript under alternative and send it to a text box, however I cannot for the life of me get that working, here's about as far as I get, with no idea on where to go from here to get to that transcript.
var bAudio = File.ReadAllBytes(file);
var memAudio = new MemoryStream(bAudio);
var results1 = speechToText.Recognize(memAudio, model: "en-US_NarrowbandModel");
Root resultsTranscription = JsonConvert.DeserializeObject<Root>(results1.Response);
I ultimately get 0 output. I can't seem to comprehend exactly how this is breaking down the JSON into it's various components
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不必让 C# 打破其自身的命名约定并遵循 JSON:
使用更复杂的网站,例如 https:// QuickType.iO(无隶属关系)来转换您的 JSON,它将为您填写这些属性,这些属性为 json 指定一个名称,为 csharp 指定另一个名称。
您还可以从文件进行流式传输,这将节省内存;不需要将文件加载到内存流中只是为了获取流(好吧,除非 Recognize 确实采用 MemoryStream;大多数基于流的东西都采用简单的 Stream,因此它们可以从提供数据流的任何内容中获取;内存、文件、网络):
然后您可以使用普通索引或 linq 样式访问:
例如,后一种形式获取置信度高于 70% 的第一个措辞。
You don't have to make your C# break its own naming conventions and follow the JSON:
Use a more sophisticated website, like https://QuickType.iO (no affiliation) to convert your JSON and it will fill in for you these properties that specify one name for the json and another for the csharp
You can also stream from a file, which would save memory; there's no need to load a file into a memory stream just to get a stream (well, unless Recognize really does take a MemoryStream; most stream based things take a simple Stream so they can be sourced from anything that's provided a stream of data; memory, file, network):
Then you can use normal indexed or linq style access:
The latter form gets the first wording whose confidence is above 70%, for example..