【发布时间】:2018-02-25 03:38:26
【问题描述】:
我正在尝试通过 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;
}
【问题讨论】:
-
Json.Net 比
JavaScriptSerializer更受欢迎,尝试使用它吗? -
是的,我正在查看它,但只是不知道如何检索该特定行。
-
如果
NBest中只有一个项目,请使用obj.NBest[0].Display,否则循环通过NBest -
谢谢! obj.DisplayText;工作!