question

41503594 avatar image
0 Votes"
41503594 asked 41503594 commented

call API and read json file

Hello,
I have a problem getting data from the JSON file that I got by calling API.
I was able to call API and get the JSON file as text but when I wanted to split the JSON file into C # variables, I couldn't.
This is the json file: https://paste.myst.rs/loj5h85d
and this is my c# script: using System; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; using Newtonsoft.Json; using System.Collections.Generic; namespace ApiCalling { public class area { public int Id { get; set; } public string Name { get; set; } public string CountryCode { get; set; } public string EnsignUrl { get; set; } } public class currentSeason { public int Id { get; set; } public string StartDate { get; set; } public string EndDate { get; set; } public int CurrentMatchday { get; set; } public string Winner { get; set; } } public class competition { public int Id { get; set; } public area Area { get; set; } public string Name { get; set; } public string Code { get; set; } public string EnsignUrl { get; set; } public string Plan { get; set; } public currentSeason CurrentSeason { get; set; } public int NumberOfAvailableSeasons { get; set; } public string LastUpdated { get; set; } } public class theComptitions { public List<competition> Competitions { get; set; } = new List<competition>(); public int Count { get; set; } } class Program { static void Main(string[] args) { RunAsync().Wait(); } static async Task RunAsync() { using (var Apiclient = new HttpClient()) { // New code: Apiclient.BaseAddress = new Uri("http://api.football-data.org/"); Apiclient.DefaultRequestHeaders.Accept.Clear(); Apiclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); Apiclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("X-Auth-token", "MyApiCode"); // GET HttpResponseMessage response = await Apiclient.GetAsync("v2/competitions"); if (response.IsSuccessStatusCode) { theComptitions combtitions = await response.Content.ReadAsAsync<theComptitions> (); Console.WriteLine(combtitions.Count); } } } } }
And I have another question: How can I get one of the combinations?


dotnet-csharp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


What errors did you get?

0 Votes 0 ·

He says that the error is in RunAsync (). Wait (); Although it is not the error

0 Votes 0 ·

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered 41503594 commented

I just de-serialized your data using Newtonsoft.Json by copying the json you provided, create a new class file, deleted the class and used Visual Studio edit, Paste Special, Paste JSON as class then saved the file. For the json I placed the code into a file, read it back using File.ReadAllText which returns a string (equivalent to your data returned)

99230-figure1.png


 using System;
 using System.IO;
 using Newtonsoft.Json;
    
 namespace Demo
 {
     public class JsonHelpers
     {
         public static TModel ReadJsonFromFile<TModel>(string fileName)
         {
             var model = default(TModel);
    
             if (!File.Exists(fileName)) return model;
                
             var json = File.ReadAllText(fileName);
             model = JsonConvert.DeserializeObject<TModel>(json);
    
             return model;
    
         }
     }
 }

Then called as follows

 var data = JsonHelpers.ReadJsonFromFile<Rootobject>("data1.json");

If using System.Text.Json the same will work

 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Text.Json;
    
 namespace JsonLibrary
 {
     public class JSonHelper
     {
         public static T DeserializeObject<T>(string json)
         {
             return JsonSerializer.Deserialize<T>(json);
         }
     }
 }






figure1.png (191.0 KiB)
· 11
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I was unable to use this method
Is there a way to use this

https://paste.myst.rs/ull8pzot

I was able to convert what I got as text with the variable json.
But what I want is how to get any data from within this variable "json".
For example, I want the Id for the first competition



0 Votes 0 ·

Here I'm using lambda



 var first = data.competitions.FirstOrDefault();
 var random = data.competitions.FirstOrDefault(competition => competition.id == 2025);
0 Votes 0 ·

I apologize, but I did not understand clearly.
s!AtzfBgwHSVNvhRHUubOiMAvwwN68
Now I was able to get the data and write a line of whatever I wanted and as shown in the picture I was able to get an ID of one of the combinations but that is not what I want.
I want to create a class with everything I want, for example I put ID and the name in it only, and then I list this class.
The reason is that there is some data that I do not need

0 Votes 0 ·
Show more comments