And*_*man 0 .net c# json json.net
在对api进行查询后,我得到了一个json响应.
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
}
Run Code Online (Sandbox Code Playgroud)
我必须对上面的json结果做两件事(我把它保存为字符串*):
处理这些字符串.
转换为字符串:我使用以下方法完成:
var reader = new StreamReader(response.GetResponseStream());
responseFromServer = reader.ReadToEnd();
Run Code Online (Sandbox Code Playgroud)
怎么做到这一点?
您可以将JSON解析为具体类,并在以后使用它们.
为此,您可以使用json2csharp之类的服务,该服务根据您提供的JSON生成类.或者,您可以使用Visual Studio内置功能粘贴JSON作为类:
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; }
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用JSON.NET将字符串化的JSON解析为具体的类实例:
var root = JsonConvert.DeserializeObject<RootObject>(responseFromServer);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3202 次 |
| 最近记录: |