解析C#中的JSON响应 [英] Parse the JSON Response in C#

查看:193
本文介绍了解析C#中的JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到作出查询,以一个API后的JSON响应

I get a json response after making a query to an api.

JSON是这样的:

    {
   "results": [
      {
         "alternatives": [
            {
               "confidence": 0.965,
               "transcript": "how do I raise the self esteem of a child in his academic achievement at the same time "
            }
         ],
         "final": true
      },
      {
         "alternatives": [
            {
               "confidence": 0.919,
               "transcript": "it's not me out of ten years of pseudo teaching and helped me realize "
            }
         ],
         "final": true
      },
      {
         "alternatives": [
            {
               "confidence": 0.687,
               "transcript": "is so powerful that it can turn bad morals the good you can turn awful practice and the powerful once they can teams men and transform them into angel "
            }
         ],
         "final": true
      },
      {
         "alternatives": [
            {
               "confidence": 0.278,
               "transcript": "you know if not on purpose Arteaga Williams who got in my mother "
            }
         ],
         "final": true
      },
      {
         "alternatives": [
            {
               "confidence": 0.621,
               "transcript": "for what pink you very much "
            }
         ],
         "final": true
      }
   ],
   "result_index": 0
}

我必须做两件事情上面JSON结果(我把它作为一个字符串*):

I have to do two things to above json result (I keep it as a string*):


  1. 获取JSON响应的成绩单部分(S)。

  2. 处理这些字符串。

  1. Get the transcript part(s) of the json response.
  2. Process those strings.


  • 我是新来这个。转换为字符串仅称为序列化。为什么要反序列化有所帮助?

转换为字符串:我做到了使用:

Converting to string: I did it using:

 var reader = new StreamReader(response.GetResponseStream());


            responseFromServer = reader.ReadToEnd();



如何实现这一点?

How to achieve this ?

推荐答案

您可以解析JSON成具体类,并与今后的工作。

You could parse the JSON into concrete classes and work with those hereafter.

要做到这一点,你可以使用像<一个服务HREF =htt​​p://json2csharp.com/相对=,它根据您提供的JSON类nofollow的> json2csharp 。或者,您可以使用Visual Studio内置功能的粘贴JSON作为类的:

To do so, you could use a service like json2csharp which generates classes based on the JSON you provided. Alternatively, you could use the Visual Studio built-in feature Paste JSON As Classes:

public class Alternative
{
    public double confidence { get; set; }
    public string transcript { get; set; }
}

public class Result
{
    public List<Alternative> alternatives { get; set; }
    public bool final { get; set; }
}

public class RootObject
{
    public List<Result> results { get; set; }
    public int result_index { get; set; }
}

您可以再使用的 JSON.NET 以字符串化的JSON解析到具体的类实例:

You can then use JSON.NET to parse the stringified JSON to concrete class instances:

var root = JsonConvert.DeserializeObject<RootObject>(responseFromServer);

这篇关于解析C#中的JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆