0
I’m new with programming and have a project in Console Application, which returns the call of an API the Json of it. I would like to know which command I can only get a part of json with. For example:
"amount": 4999,
"currency": "BRL",
"closed": true,
"items": [
- Just take the field
amount: 4999,
and save in a variable
Follow some information regarding the code, if you need any more information please let me know:
Method of the API call that returns Json
- The
console.write(response.content)
returns the json that comes from the API, similar to the above example.
public class GetOrder
{
[HttpGet]
public static void GetOrderId(string token, string url)
{
Authenticator auth = new Authenticator();
Authenticator.Authorization();
try
{
var client = new RestClient(url);
//var request = new RestRequest(("orders/" + orderId), Method.GET, DataFormat.Json);
var request = new RestRequest(("orders/"), Method.GET, DataFormat.Json);
client.Authenticator = new HttpBasicAuthenticator(token, auth.BasicAuthPassword);
var response = client.Get(request);
client.Execute(request);
Console.WriteLine(response.Content);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
Program.Cs
static void Main(string[] args)
{
// Le o appsettings e criar um objeto IConfiguration
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false)
.Build();
// Lê o valor da chave
var token = config.GetSection("Config:ApiTokenSecretQA").Value;
var url = config.GetSection("Config:UrlApi").Value;
// Passa como parâmetro
GetOrder.GetOrderId(token, url);
//GetOrder.GetOrderId();
//CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddHostedService<Worker>();
});
How to exit the command
Console.WriteLine(response.Content);
?– Henrique Miranda
Welcome to the Sopt. I suggest you reread your question and try to rewrite it to explain more clearly what you intend to do and what problem you face. Besides the question is not clear, the code presented also does not make much sense.
– tvdias
@Henriquemiranda mentioned above, I edited the post.
– Gabriel Rio
@tvdias I edited the post, see if it got better to understand. I don’t know which part of the code can be more useful. The very question is how to save, for example, only a json line or only the value of a json field in a variable. It was clear?
– Gabriel Rio