C# 打印JSON响应中的特定值
我刚刚又开始玩C了,我无法克服这个问题 我有以下JSON内容:C# 打印JSON响应中的特定值,c#,json,C#,Json,我刚刚又开始玩C了,我无法克服这个问题 我有以下JSON内容: { "type": "success", "picks": [ { "aftermarket": { "domain": "ao38t8u4h.com", "fast_transfer": false, "price": 0, "status": "notfound", "type": "", "usernam
{
"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中要做的是得到响应,然后简单地打印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();
}
}
}
}
除了@mjwills:
反序列化json。然后按如下方式打印值:
Console.WriteLine($"Value = {json.Picks[0].Status.Premium}");
除了@mjwills:
反序列化json。然后按如下方式打印值:
Console.WriteLine($"Value = {json.Picks[0].Status.Premium}");
Json.Net支持您使用的语法
Fiddle:Json.Net支持您使用的语法 Fiddle:-99%的JSON问题通过首先将其反序列化为具体类型来解决99%的JSON问题更简单的解决方法是首先将它们反序列化为具体类型。