gpt4 book ai didi

c# - 在 json WebResponse c# 中检索特定行

转载 作者:行者123 更新时间:2023-11-30 21:35:43 26 4
gpt4 key购买 nike

我正在尝试通过 WebResponse 方法检索 json 请求中的特定行,但目前无法正常工作。我正在将 wav 文件音频转换为文本。它工作良好并且转换得很好,但我得到输出行:RecognitionStatus、Duration、Display、Offset。我想要输出到文本框的唯一行是音频到文本转换到的“显示”行。

json 格式看起来像这样,我试图只获取“显示”行。

{
"RecognitionStatus": "Success",
"Offset": 22500000,
"Duration": 21000000,
"NBest": [{
"Confidence": 0.941552162,
"Lexical": "find a funny movie to watch",
"ITN": "find a funny movie to watch",
"MaskedITN": "find a funny movie to watch",
"Display": "Find a funny movie to watch."
}]
}

这是我目前的代码。

        HttpWebRequest request = null;
string ResponseString;
request = (HttpWebRequest)HttpWebRequest.Create("https://speech.platform.bing.com/speech/recognition/dictation/cognitiveservices/v1?language=en-US&format=simple");
request.SendChunked = true;
request.Accept = @"application/json;text/xml";
request.Method = "POST";
request.ProtocolVersion = HttpVersion.Version11;
request.ContentType = @"audio/wav; codec=audio/pcm; samplerate=16000";
request.Headers["Ocp-Apim-Subscription-Key"] = "hidden";

using (FileStream fs = new FileStream(@"G:\Microsoft Visual Studio Projects\SpeechRecognitionFormsTestUpdaterad\SpeechRecognitionForms\bin\Debug\Logs\log 24-2.wav", FileMode.Open, FileAccess.Read))
{
byte[] buffer = null;
int bytesRead = 0;
using (Stream requestStream = request.GetRequestStream())
{
/*
* Read 1024 raw bytes from the input audio file.
*/

buffer = new Byte[checked((uint)Math.Min(1024, (int)fs.Length))];
while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
}

// Flush
requestStream.Flush();
}
}
using (WebResponse response = request.GetResponse())
{
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
ResponseString = sr.ReadToEnd();
JavaScriptSerializer js = new JavaScriptSerializer();
MyObject obj = (MyObject)js.Deserialize(ResponseString, typeof(MyObject));
textBox1.Text = ResponseString;
}
//textBox1.Text = ResponseString;
}

最佳答案

因为 NBest 将是一个集合。您必须迭代以获取 Display 的每个值。

您可以像这样检索 Display 的值:

using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
ResponseString = sr.ReadToEnd();
JavaScriptSerializer js = new JavaScriptSerializer();
MyObject obj = (MyObject)js.Deserialize(ResponseString, typeof(MyObject));
textBox1.Text = ResponseString;
foreach (var nb in obj.NBest)
{
Console.WriteLine(nb.Display);
}
}

或者如果你总是得到一个单一的 NBest 对象,你可以像这样检索它:

if (obj.NBest.Count == 1)
{
string display = obj.NBest[0].Display;
}

更新:

以下是我将 OP 的 JSON 反序列化为的类:

public class MyObject
{
public string RecognitionStatus { get; set; }
public int Offset { get; set; }
public int Duration { get; set; }
public List<Nbest> NBest { get; set; }
}

public class Nbest
{
public float Confidence { get; set; }
public string Lexical { get; set; }
public string ITN { get; set; }
public string MaskedITN { get; set; }
public string Display { get; set; }
}

输出:

enter image description here

关于c# - 在 json WebResponse c# 中检索特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48964002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com