Betfair API : Read Betfair JSON in C# into human readable form

Post Reply
footyman2
Posts: 6
Joined: Fri May 11, 2018 11:31 am

Fri Jun 08, 2018 3:18 pm

I'm at early stages with this, I also find this webpage useful, bit more readable. Let me know if you find it useful so far, you should get the gist if you have some programming knowledge.

Please ask if anything below seems scary. It really isn't.

http://jsonviewer.stack.hu/

If anyone has classes set up to deserialise to, do please let me know, please feel free to augment. Make it a bit more human friendly, plus not used dynamic objects in anger much as yet still playing.

1) Download the football sample, and unzip using 7zip, I think winzip works too.
2) Download Linqpad it is free - and awesome.
3) Download Newtonsoft dll. The below code needs a reference to it. In Linqpad press F4, click browse, and where you downloaded this to and add the dll, it is in a subfolder called Json110r2\Bin\net45\Newtonsoft.Json.dll
4) Paste the following into the window. It will complain, and ask you to add a reference. If not look on Linqpad website on how to do all that.
and press F5 to run.

This should output
Middlesbrough v Man City
28202626
Over/Under 0.5 Goals
30/04/2017 13:05:00




string[] file = File.ReadAllLines(@"C:\temp\football-basic-sample");

//read the first record
foreach(var line in file.Take(1))
{
dynamic stuff = JsonConvert.DeserializeObject(line);

Console.WriteLine(stuff.mc[0].marketDefinition.eventName);
Console.WriteLine(stuff.mc[0].marketDefinition.eventId);
Console.WriteLine(stuff.mc[0].marketDefinition.name);

Console.WriteLine(stuff.mc[0].marketDefinition.openDate);

}

footyman2
Posts: 6
Joined: Fri May 11, 2018 11:31 am

Fri Jun 08, 2018 5:53 pm

Ok this isn't pretty but I have run out of time for today.

The below lists all home win prices and the time. Might have bugs.

Change to run C# program if running in Linqpad.

void Main()
{
string[] file = File.ReadAllLines(@"C:\temp\football-basic-sample");

string homeWinID = string.Empty;

//read the first record
foreach(var line in file.Take(1))
{
dynamic stuff = JsonConvert.DeserializeObject(line);

int market = 2; // match odds

Console.WriteLine(stuff.mc[market].marketDefinition.marketType);
Console.WriteLine(stuff.mc[market].marketDefinition.eventName);
Console.WriteLine("Event id " + stuff.mc[market].marketDefinition.eventId);
Console.WriteLine("Market id " + stuff.mc[market].id);
homeWinID = stuff.mc[market].marketDefinition.runners[0].id;
Console.WriteLine("Home Win id " + stuff.mc[market].marketDefinition.runners[0].id);
Console.WriteLine("Away Win id " + stuff.mc[market].marketDefinition.runners[1].id);
Console.WriteLine("Draw id " + stuff.mc[market].marketDefinition.runners[2].id);
Console.WriteLine(stuff.mc[market].marketDefinition.name);

Console.WriteLine(stuff.mc[market].marketDefinition.openDate);

}

//read data lines
foreach(var line in file.Skip(1))
{
dynamic stuff = JsonConvert.DeserializeObject(line);

//Console.WriteLine(stuff);

if (line.Contains(homeWinID) && !line.Contains("marketDefinition"))
{
//string time = JavaTimeStampToDateTime(double.Parse(stuff.pt).ToString());

string javDt = stuff.pt.ToString();
DateTime dt = JavaTimeStampToDateTime(double.Parse(javDt));
string time = stuff.pt;
Console.WriteLine(dt.ToString() + "," + stuff.mc[0].rc[0].ltp);
}

}

}

// Define other methods and classes here
public static DateTime JavaTimeStampToDateTime( double javaTimeStamp )
{
// Java timestamp is milliseconds past epoch
System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddMilliseconds( javaTimeStamp ).ToLocalTime();
return dtDateTime;
}

dougkpga
Posts: 11
Joined: Wed Aug 22, 2018 4:12 pm

Wed Aug 22, 2018 4:32 pm

Great work, although I am new to this.

I got a spreadsheet made with macros that imports the csv files from football-data.co.uk

Post Reply

Return to “Betfair API”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 0 guests