2012-07-24 66 views
6

我创建一个C#Windows应用程序使用必发交换网络服务来显示当前体育市场的价格,我用了有没有人有使用betfair api的c#代码?

getmarketpricescompressed()

方法返回一个价格字符串,看起来像这样:

106093239~GBP~ACTIVE~0~1~~true~5.0~1343114432333~~N:7337~1~6992.56~2.16~~~false~~~~|2.16~1036.19~L~1~2.14~97.18~L~2~2.12~5.0~L~3~|2.18~467.36~B~1~2.2~34.12~B~2~2.22~162.03~B~3~:414464~2~102181.96~1.86~~~false~~~~|1.85~2900.33~L~1~1.84~1831.59~L~2~1.83~1593.73~L~3~|1.86~58.83~B~1~1.87~1171.77~B~2~1.88~169.15~B~3~ 

我不知道如何正确地解开这个字符串,现在我使用此代码:

GetMarketPricesCompressedReq price_req1 = new GetMarketPricesCompressedReq(); 
      price_req1.header = header2; 
      price_req1.marketId = marketid_temp; 
      price_req1.currencyCode = "GBP"; 
      GetMarketPricesCompressedResp price_resp = new GetMarketPricesCompressedResp(); 
      price_resp = bfg2.getMarketPricesCompressed(price_req1); 
      //MessageBox.Show(price_resp.errorCode.ToString()); 
      //richTextBox1.Text = ""; 
      //richTextBox1.Text = price_resp.marketPrices; 
      string prices = price_resp.marketPrices; 
      richTextBox1.Text = price_resp.marketPrices; 
      string[] ab1 = prices.Split('|'); 
      string[] temp = ab1[1].Split('~'); 
      textBox3.Text = temp[0]; 
      textBox4.Text = temp[4]; 
      textBox5.Text = temp[8]; 
      temp = ab1[2].Split('~'); 
      textBox6.Text = temp[0]; 
      textBox7.Text = temp[4]; 
      textBox8.Text = temp[8]; 
      temp = ab1[3].Split('~'); 
      textBox9.Text = temp[0]; 
      textBox10.Text = temp[4]; 
      textBox11.Text = temp[8]; 
      temp = ab1[4].Split('~'); 
      textBox12.Text = temp[0]; 
      textBox13.Text = temp[4]; 
      textBox14.Text = temp[8]; 
      if (ab1.Length >5) 
      { 
       temp = ab1[5].Split('~'); 
       textBox15.Text = temp[0]; 
       textBox16.Text = temp[4]; 
       textBox17.Text = temp[8]; 
       temp = ab1[6].Split('~'); 
       textBox18.Text = temp[0]; 
       textBox19.Text = temp[4]; 
       textBox20.Text = temp[8]; 
      } 

它适用于几个匹配,但我观察到一些其他匹配的字符串变化,因此会产生异常, 任何1可以帮助我一个正确的代码来解压这个字符串,我GOOGLE了它,发现了一个VB代码,这是不是非常有用的,

和BTW,我想安排在一些数据是这样的:

enter image description here

+0

该方法返回一个字符串,价格看起来像一个长谷歌的网址? – Jodrell 2012-07-24 07:17:35

+0

oh gosh,对不起,这是一个错误,我纠正它是真实的:3 – 2012-07-24 07:19:08

+0

(大概)VB.NET代码有什么问题?如果它确实是VB.NET,那么它可以很容易地转换成C#,只需一点技巧。 =)另外,考虑将第二个问题作为第二个问题发布。 =) – 2012-07-24 07:24:06

回答

2

只是猜测,我认为数据组织这样

首先:分隔然后|我们得到

106093239〜GBP〜ACTIVE〜0〜1 ~~ true〜5.0〜1343114432333 ~~ N

7337〜1〜6992.56〜2.16 ~~~ false ~~~~
2.16〜1036.19〜L〜1〜2.14〜97.18〜L〜2〜2.12〜5.0〜L〜3〜 2.18〜467.36〜B〜1〜2.2〜34.12〜B〜2〜2.22〜162.03〜B〜3 〜

414464〜2〜102181.96〜1.86 ~~~假~~~~ 1.85〜2900.33〜L〜1〜1.84〜1831.59〜L〜2〜1.83〜1593.73〜1〜3〜 1.86〜58.83〜乙〜1〜1.87〜1171.77〜B〜2〜1.88〜169.15〜B〜3〜

我认为第一行显然是一个标头,该第一数量的市场ID也许。同样,每个后续部分的第一部分是行标识符。通过~像分隔

(SelectionId)7337(订单)1(TotalMatched?)6992.56(EvenPoint)2.16(???)假

我认为价格的行由~在分隔的4元组,这样

(价格)2.16(MatchAvailable)1036.19(类型):L(顺序)1
(价格)2.14(MatchAvailable)0097.18(类型):L(订单)2
(价格)2.12(Match Available)0005.00(Type)L(Order)3

例如。

为了测试我的猜测,我必须与他们网站上的BetFair渲染进行比较。

+0

根据文件,这是正确的。 =) – 2012-07-24 08:08:33

+0

显然,尽可能接近网站上显示的原始数据,(价格)和(MatchAvailable)如网站所示, – 2012-07-24 10:34:55

3

考虑以下几点:

a|b|c|d 

在这里,你有四个块。但它不一定只有四个。它可以是两个,也可以是六个。

尝试它会出示您的数据,以及使用更动态的方法考虑遍历字符串数组你作为

string[] ab1 = prices.Split('|'); 

喜欢的东西

foreach(var stringChunk in ab1) 
{ 
    string[] temp = stringChunk.Split('~'); 
    // use the strings here as you please 
} 

的结果,使用基于行的重复控件而不是静态文本框。 =)

的压缩数据的完整文档可以在这里找到:https://docs.developer.betfair.com/betfair/#!page=00008360-MC.00008307-MC

+0

以及这似乎是合法的,但如果我不得不在标签上显示解析的数据,那么我将不得不生成和定位这些标签动态......但我想这是值得一拍...... thanxx – 2012-07-24 07:37:25

2

我转换VB代码到C#我自己,如果有人可能会发现它很有用,我在这里张贴:

using Microsoft.VisualBasic; 
using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Data; 
using System.Diagnostics; 

namespace stock 
{ 
    class UnpackMarketPricesCompressed : stock.BFExchangeService.MarketPrices 
{ 
     //The substitute code for "\:" 
    private const string ColonCode = "&%^@"; 

    //Unpack the string 
    public UnpackMarketPricesCompressed(string MarketPrices) 
    { 
     string[] Mprices = null; 
     string[] Part = null; 
     string[] Field = null; 
     int n = 0; 

     Mprices = MarketPrices.Replace("\\:", ColonCode).Split(':'); 
     //Split header and runner data 
     Field = Mprices[0].Replace("\\~", "-").Split('~'); 
     //Split market data fields 
     marketId = Convert.ToInt32(Field[0]); 
     //Assign the market data 
     currencyCode = Field[1]; 
     marketStatus = (stock.BFExchangeService.MarketStatusEnum)Enum.Parse(typeof(stock.BFExchangeService.MarketStatusEnum), Field[2], true); 
     delay = Convert.ToInt32(Field[3]); 
     numberOfWinners = Convert.ToInt32(Field[4]); 
     marketInfo = Field[5].Replace(ColonCode, ":"); 
     discountAllowed = (Field[6].ToLower() == "true"); 
     marketBaseRate = float.Parse(Field[7]); 
     lastRefresh = long.Parse(Field[8]); 
     removedRunners = Field[9].Replace(ColonCode, ":"); 
     bspMarket = (Field[10] == "Y"); 

     n = Mprices.Length - 1; 
     // ERROR: Not supported in C#: ReDimStatement 

     //For each runner 
     for (int i = 0; i <= n; i++) 
     { 
      Part = Mprices[i + 1].Split('|'); 
      //Split runner string into 3 parts 
      Field = Part[0].Split('~'); 
      //Split runner data fields 
      runnerPrices[i] = new stock.BFExchangeService.RunnerPrices(); 
      var _with1 = runnerPrices[i]; 
      //Assign the runner data 
      _with1.selectionId = Convert.ToInt32(Field[0]); 
      _with1.sortOrder = Convert.ToInt32(Field[1]); 
      _with1.totalAmountMatched = Convert.ToDouble(Field[2]); 
      _with1.lastPriceMatched = Convert.ToDouble(Field[3]); 
      _with1.handicap = Convert.ToDouble(Field[4]); 
      _with1.reductionFactor = Convert.ToDouble(Field[5]); 
      _with1.vacant = (Field[6].ToLower() == "true"); 
      _with1.farBSP = Convert.ToDouble(Field[7]); 
      _with1.nearBSP = Convert.ToDouble(Field[8]); 
      _with1.actualBSP = Convert.ToDouble(Field[9]); 
      _with1.bestPricesToBack = Prices(Part[1]); 
      _with1.bestPricesToLay = Prices(Part[2]); 
     } 
    } 

    private stock.BFExchangeService.Price[] Prices(string PriceString) 
    { 
     string[] Field = null; 
     stock.BFExchangeService.Price[] Price = null; 
     int k = 0; 
     int m = 0; 

     Field = PriceString.Split('~'); 
     //Split price fields 
     m = (Field.Length/4) - 1; 
     //m = number of prices - 1 
     // ERROR: Not supported in C#: ReDimStatement 

     for (int i = 0; i <= m; i++) 
     { 
      Price[i] = new stock.BFExchangeService.Price(); 
      var _with2 = Price[i]; 
      _with2.price = Convert.ToInt32(Field[k + 0]); 
      //Assign price data 
      _with2.amountAvailable = Convert.ToInt32(Field[k + 1]); 
      _with2.betType = (stock.BFExchangeService.BetTypeEnum)Enum.Parse(typeof(stock.BFExchangeService.BetTypeEnum), Field[k + 2], true); 
      _with2.depth = Convert.ToInt32(Field[k + 3]); 
      k += 4; 
     } 
     return Price; 
     //Return the array of prices 
    } 

    } 
} 
相关问题