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

标签 c# json

我正在尝试通过 WebResponse 方法检索 json 请求中的特定行,但目前无法正常工作。我正在将 wav 文件音频转换为文本。它工作得很好并且转换得很好,但我得到了输出行:识别状态、持续时间、显示、偏移。我想要输出到文本框的唯一行是音频到文本转换到的“显示”行。

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/

相关文章:

c# - 根据角色创建类

c# - String.Format 未按预期工作 C#

c++ - 存储为 UTF-8 的 JSON 需要两次编码转换

python - 如何为下面的字典创建 json 字符串

javascript - 我找不到打印数组的方法

java - 使用 Jackson 与 JaxB 注释进行 Json 映射

c# - Linq to entities if else 条件查询

c# - 我如何订购 SOAP 请求 header ?

c# - 可以将 C# 枚举声明为 bool 类型吗?

ruby-on-rails - Ruby on Rails : `no implicit conversion of String into Integer` for getting json field