API-FOOTBALL

FREEMIUM
Verified
By API-SPORTS | Updated 9 giorni fa | Sports
Popularity

10 / 10

Latency

359ms

Service Level

99%

Health Check

N/A

Back to All Discussions

Json Serialization

Rapid account: Karyelist
karyelist
6 anni fa

hi,

I can read json. For example json

{
“api”: {
“results”: 4,
“fixtures”: {
“1757”: {
“fixture_id”: “1757”,
“event_timestamp”: “1532565900”,
“league_id”: “6”,
“round”: “Serie A - 15”,
“homeTeam_id”: “131”,
“awayTeam_id”: “135”,
“homeTeam”: “Corinthians”,
“awayTeam”: “Cruzeiro”,
“status”: “Match Finished”,
“goalsHomeTeam”: “2”,
“goalsAwayTeam”: “0”,
“elapsed”: “0”,
“firstHalfStart”: “0”,
“secondHalfStart”: “0”
},
“1758”: {
“fixture_id”: “1758”,
“event_timestamp”: “1532565900”,
“league_id”: “6”,
“round”: “Serie A - 15”,
“homeTeam_id”: “128”,
“awayTeam_id”: “127”,
“homeTeam”: “Santos”,
“awayTeam”: “Flamengo”,
“status”: “Match Finished”,
“goalsHomeTeam”: “1”,
“goalsAwayTeam”: “1”,
“elapsed”: “0”,
“firstHalfStart”: “1532611202”,
“secondHalfStart”: “0”
},
“1759”: {
“fixture_id”: “1759”,
“event_timestamp”: “1532563200”,
“league_id”: “6”,
“round”: “Serie A - 15”,
“homeTeam_id”: “117”,
“awayTeam_id”: “122”,
“homeTeam”: “Atletico Mineiro”,
“awayTeam”: “Parana”,
“status”: “Match Finished”,
“goalsHomeTeam”: “2”,
“goalsAwayTeam”: “0”,
“elapsed”: “0”,
“firstHalfStart”: “0”,
“secondHalfStart”: “0”
},
“1760”: {
“fixture_id”: “1760”,
“event_timestamp”: “1532557800”,
“league_id”: “6”,
“round”: “Serie A - 15”,
“homeTeam_id”: “124”,
“awayTeam_id”: “121”,
“homeTeam”: “Fluminense”,
“awayTeam”: “Palmeiras”,
“status”: “Match Finished”,
“goalsHomeTeam”: “1”,
“goalsAwayTeam”: “0”,
“elapsed”: “0”,
“firstHalfStart”: “0”,
“secondHalfStart”: “0”
}
}
}
}

But I can not create Serialization class. Because some places are dynamic numbers. If you has a class structure example, please share with me. Can you help me ?

Rapid account: Api Sports
api-sports Commented 6 anni fa

Ok cool

Rapid account: Karyelist
karyelist Commented 6 anni fa

I founded solution.

HttpClient tarayici = new HttpClient();

    string yil = DateTime.Now.Year.ToString();
    string ay = DateTime.Now.Month.ToString();
    string gun = DateTime.Now.Day.ToString();


    int gunu = Convert.ToInt32(gun);
    gunu = gunu + 1;
   // var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api-football-v1.p.mashape.com/odds/{fixture_id}");
    var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api-football-v1.p.mashape.com/fixtures/date/2018-08-15");


    httpWebRequest.ContentType = "application/json";


    httpWebRequest.ContentLength = 0;

    httpWebRequest.Headers["X-Mashape-Key"] = "xxxx";
    httpWebRequest.Accept = "application/json";

    string[] dizi;
    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var responseText = streamReader.ReadToEnd();
        
        JObject googleSearch = JObject.Parse(responseText);

var responseText = streamReader.ReadToEnd();
responseText = responseText.ToString().Replace(""", “’”);
JObject googleSearch = JObject.Parse(responseText);

            string json = responseText;


            JObject obj = JObject.Parse(json);
            var attributes = obj["api"]["fixtures"].ToList<JToken>();

            foreach (JToken attribute in attributes)
            {
                JProperty jProperty = attribute.ToObject<JProperty>();
                string propertyName = jProperty.Name;
                string propertyValue = jProperty.Value.ToString();

                foreach (var tempToken in attributes)
                {
                    JObject obj2 = JObject.Parse("{" + tempToken.ToString() + "}");
                    foreach (var pair in obj2)
                    {

                        Console.WriteLine(pair.Key + "," + pair.Value);
                        json = Convert.ToString(pair).Replace("\r\n ", "");


                        int sonuc;
                        string ara = ", {";
                        sonuc = json.IndexOf(ara, 0, json.Length - 1);
                        sonuc = sonuc + 2;
                        json = json.Remove(0, sonuc);
                        json = json.Replace("]", "");
                        RootObject3 r = new RootObject3();
                        JObject obj4 = JObject.Parse(json);
                        foreach (var pair2 in obj4)
                        {

                            int sayi = 0;
                         

                            if (pair2.Key == "fixture_id")
                            {
                                r.fixture_id = pair2.Value.ToString();
                            }

and others…

thanks.

Rapid account: Karyelist
karyelist Commented 6 anni fa

Ok. I changed.

Rapid account: Api Sports
api-sports Commented 6 anni fa

Hi,

Please edit your comment, your API KEY is visible !

Rapid account: Karyelist
karyelist Commented 6 anni fa

hello, dynamic numbers is fixture_id at the beginning of a sentence. For example I need convert json to class for insert database. Do you have example class ?

My code is ;

public class RootObject3

    {

        public string fixture_id { get; set; }

        public string event_timestamp { get; set; }

        public string league_id { get; set; }

        public string round { get; set; }

        public string homeTeam_id { get; set; }

        public string awayTeam_id { get; set; }

        public string homeTeam { get; set; }

        public string awayTeam { get; set; }

        public string status { get; set; }

        public string goalsHomeTeam { get; set; }

        public string goalsAwayTeam { get; set; }

        public string elapsed { get; set; }

        public string firstHalfStart { get; set; }

        public string secondHalfStart { get; set; }

    }

public void al()
{

        HttpClient tarayici = new HttpClient();

        string yil = DateTime.Now.Year.ToString();
        string ay = DateTime.Now.Month.ToString();
        string gun = DateTime.Now.Day.ToString();


        int gunu = Convert.ToInt32(gun);
        gunu = gunu + 1;
       // var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api-football-v1.p.mashape.com/odds/{fixture_id}");
        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api-football-v1.p.mashape.com/fixtures/date/2018-08-15");

        
        httpWebRequest.ContentType = "application/json";

      
        httpWebRequest.ContentLength = 0;
  
        httpWebRequest.Headers["X-Mashape-Key"] = "xxxx";
        httpWebRequest.Accept = "application/json";

        string[] dizi;
        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var responseText = streamReader.ReadToEnd();
            responseText = responseText.ToString().Replace("\"", "'");
            JObject googleSearch = JObject.Parse(responseText);


            dynamic dyn = JObject.Parse(responseText);
          
            IList<JToken> results = googleSearch["api"]["fixtures"].Children().ToList();

            // serialize JSON results into .NET objects
            IList<RootObject3> searchResults = new List<RootObject3>();
            foreach (JToken result in results)
            {
                string m = result.ToString();
             
                RootObject3 searchResult = result.ToObject<RootObject3>();
                searchResults.Add(searchResult);


                //int sonuc;
                //string ara = ": {";
                //sonuc = m.IndexOf(ara, 0, m.Length - 1);
                //sonuc = sonuc + 2;
                //m = m.Remove(0, sonuc);
                //m = m.Replace("\"", "'");


                var myJsonResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<IList<RootObject3>>(m);



            }
        }





    }

thanks

Rapid account: Karyelist
karyelist Commented 6 anni fa

hello,
dynamic numbers is fixture_id at the beginning of a sentence. For example
I need convert json to class for insert database. Do you have example class ?

My code is ;

HttpClient tarayici = new HttpClient();

        string yil = DateTime.Now.Year.ToString();
        string ay = DateTime.Now.Month.ToString();
        string gun = DateTime.Now.Day.ToString();


        int gunu = Convert.ToInt32(gun);
        gunu = gunu + 1;
       // var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api-football-v1.p.mashape.com/odds/{fixture_id}");
        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api-football-v1.p.mashape.com/fixtures/date/2018-08-15");

        
        httpWebRequest.ContentType = "application/json";

      
        httpWebRequest.ContentLength = 0;
  
        httpWebRequest.Headers["X-Mashape-Key"] = "xxx";
        httpWebRequest.Accept = "application/json";

        string[] dizi;
        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var responseText = streamReader.ReadToEnd();
            responseText = responseText.ToString().Replace("\"", "'");
            JObject googleSearch = JObject.Parse(responseText);


            dynamic dyn = JObject.Parse(responseText);
          
            IList<JToken> results = googleSearch["api"]["fixtures"].Children().ToList();

            // serialize JSON results into .NET objects
            IList<RootObject3> searchResults = new List<RootObject3>();
            foreach (JToken result in results)
            {
                string m = result.ToString();
             
                RootObject3 searchResult = result.ToObject<RootObject3>();
                searchResults.Add(searchResult);


            


            }

public class RootObject3
{
public string fixture_id { get; set; }
public string event_timestamp { get; set; }
public string league_id { get; set; }
public string round { get; set; }
public string homeTeam_id { get; set; }
public string awayTeam_id { get; set; }
public string homeTeam { get; set; }
public string awayTeam { get; set; }
public string status { get; set; }
public string goalsHomeTeam { get; set; }
public string goalsAwayTeam { get; set; }
public string elapsed { get; set; }
public string firstHalfStart { get; set; }
public string secondHalfStart { get; set; }
}

Thanks

Rapid account: Api Sports
api-sports Commented 6 anni fa

Hello,

what are you trying to do exactly?

For example :

1757 : is the ID of the matche (it is unique)

“fixtures”: { “1757”: { “fixture_id”: “1757”, “event_timestamp”: “1532565900”, “league_id”: “6”, “round”: “Serie A - 15”, “homeTeam_id”: “131”, “awayTeam_id”: “135”, “homeTeam”: “Corinthians”, “awayTeam”: “Cruzeiro”, “status”: “Match Finished”, “goalsHomeTeam”: “2”, “goalsAwayTeam”: “0”, “elapsed”: “0”, “firstHalfStart”: “0”, “secondHalfStart”: “0” },

What are the “dynamic numbers” ?

For betters support contact us at : apifootball.contact@gmail.com

Join in the discussion - add comment below:

Login / Signup to post new comments