c# - 从 JSON 响应中打印特定值
问题描述
刚开始再次使用 C#,我无法克服这个问题。
我有以下 JSON 内容:
{
"type": "success",
"picks": [
{
"aftermarket": {
"domain": "ao38t8u4h.com",
"fast_transfer": false,
"price": 0,
"status": "notfound",
"type": "",
"username": ""
},
"domain": "ao38t8u4h.com",
"info": "",
"priority": 1,
"status": {
"available": true,
"lookupType": "EPP",
"name": "ao38t8u4h.com",
"premium": false
},
"tld": "com",
"type": "domain"
},
{
"aftermarket": {
"domain": "ao38t8u4h.net",
"fast_transfer": false,
"price": 0,
"status": "notfound",
"type": "",
"username": ""
},
"domain": "ao38t8u4h.net",
"info": "",
"priority": 2,
"status": {
"available": true,
"lookupType": "EPP",
"name": "ao38t8u4h.net",
"premium": false
},
"tld": "net",
"type": "domain"
},
{
"aftermarket": {
"domain": "ao38t8u4h.dev",
"fast_transfer": false,
"price": 0,
"status": "notfound",
"type": "",
"username": ""
},
"domain": "ao38t8u4h.dev",
"info": "",
"priority": 3,
"status": {
"available": true,
"lookupType": "EPP",
"name": "ao38t8u4h.dev",
"premium": false
},
"tld": "dev",
"type": "domain"
},
{
"aftermarket": {
"domain": "ao38t8u4h.ai",
"fast_transfer": false,
"price": 0,
"status": "notfound",
"type": "",
"username": ""
},
"domain": "ao38t8u4h.ai",
"info": "",
"priority": 4,
"status": {
"available": true,
"lookupType": "EPP",
"name": "ao38t8u4h.ai",
"premium": false
},
"tld": "ai",
"type": "domain"
},
{
"aftermarket": {
"domain": "ao38t8u4h.org",
"fast_transfer": false,
"price": 0,
"status": "notfound",
"type": "",
"username": ""
},
"domain": "ao38t8u4h.org",
"info": "",
"priority": 5,
"status": {
"available": true,
"lookupType": "EPP",
"name": "ao38t8u4h.org",
"premium": false
},
"tld": "org",
"type": "domain"
}
]
}
我想做的是获得["picks"][0]["status"]["premium"]
. 我在 Python 中所做的是获取响应,然后简单print(var["picks"][0]["status"]["premium"])
地打印值;但是,我不知道如何在 C# 中完成此操作。
我正在使用的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net.Http;
using Newtonsoft.Json;
namespace testing
{
class Program
{
private static readonly HttpClient client = new HttpClient();
static async Task Main(string[] args)
{
try
{
string domain = "ao38t8u4h";
string url = "https://rtb.namecheapapi.com/api/picks/" + domain;
string responseBody = await client.GetStringAsync(url);
var json = JsonConvert.DeserializeObject(responseBody);
Console.WriteLine(json);
Console.ReadLine();
}
catch(HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ", e.Message);
Console.ReadLine();
}
}
}
}
解决方案
Json.Net 支持您习惯使用的语法,如果您使用JTokens
.
JToken jt = JToken.Parse(responseBody);
Console.WriteLine(jt["picks"][0]["status"]["premium"]);
推荐阅读
- python - 从 python 导出的模型在 C++ 中加载时会给出不同的结果
- visual-studio-code - 在 Visual Studio Code 中安装 Vercel CLI
- swift - 如何使用 Nimble 测试通知的 userInfo 部分?
- c - Fortran 与 C 函数指针的互操作结果错误
- mysql - MySQL如何统计最近7天的访问量
- php - AWS EC2 实例上的 Nginx + Php Fpm 扩展问题
- python - UDP 套接字连接在接收时被拒绝
- azure-devops - Azure DevOps 管道 kubectl 任务命令:部署不可用
- amazon-web-services - 如何在 aws 数据管道的 shell 脚本中读取 ssm 参数?
- sql - GROUP BY 后 CTE 中的 ORDER 丢失