с сайта whattomine.com
c использование сеарилизатора при парсинге JSON
TaskProfit := TTask.Run(procedure()
begin
var ProfMode: SmallInt;
TThread.Synchronize(Nil, procedure
begin
ProfMode := FProfitMode;
end);
var ResponseStr: String;
var Url: String;
var hr : string; // hashrate; hr=16
var curr: string; // &cost_currency=USD
hr := 'hr=100';
curr := '&cost_currency=USD';
var HTTPClient := THTTPClient.Create;
HTTPClient.ConnectionTimeout := 10000;
HTTPClient.Accept := CONTENTTYPE_APPLICATION_JSON;
HTTPClient.AcceptCharSet := 'UTF-8';
try
// ---------------------- Get Exchanhe XMR --------------------------
if ProfMode = 1 then
begin
Url := 'https://whattomine.com/coins/' + cXMRCode + '.json?' + hr +
'&p=0&fee=0&cost=0' + curr + '&hcost=0&span_br=&span_d=24';
Try
ResponseStr := HTTPClient.Get(Url).ContentAsString;
var CoinXMRPtofit := TJson.JsonToObject<TCoinProfit>(ResponseStr, [joIndentCasePreserve]);
if Assigned(CoinXMRPtofit) then
TThread.Synchronize(Nil, procedure
begin
CoinsProfitList.AddOrSetValue(cXMR, CoinXMRPtofit);
end);
Except
on E: Exception do
begin
TThread.Synchronize(Nil, procedure
begin
log('Error: ' + E.Message + ' metod: TimerGetExchangeInfoTimer');
end);
end;
End;
end; // end if
finally
HTTPClient.Free;
end;
end);
Например, создаем класс: TCoinProfit = class Private Fid : UInt16; Fname : string; Ftag : string; Falgorithm : string; Fblock_time : string; Fblock_reward : Float64; Fblock_reward24 : Float64; Fblock_reward3 : Float64; Fblock_reward7 : Float64; Flast_block : UInt64; Fdifficulty : UInt64; Fdifficulty24 : Float64; Fdifficulty3 : Float64; Fdifficulty7 : Float64; Fnethash : UInt64; Fexchange_rate : Float64; Fexchange_rate24 : Float64; Fexchange_rate3 : Float64; Fexchange_rate7 : Float64; Fexchange_rate_vol : Float64; Fexchange_rate_curr : string; Fmarket_cap : string; Fpool_fee : string; Festimated_rewards : string; Fbtc_revenue : string; Frevenue : string; Fcost : string; Fprofit : string; Fstatus : string; Flagging : boolean; Ftesting : boolean; Flisted : boolean; Ftimestamp : UInt32; FResponseTime : TDateTime; public property id: UInt16 read Fid; property name: string read Fname; property tag: string read Ftag; property algorithm: string read Falgorithm; property block_time: string read Fblock_time; property block_reward: Float64 read Fblock_reward; property block_reward24: Float64 read Fblock_reward24; property block_reward3: Float64 read Fblock_reward3; property block_reward7: Float64 read Fblock_reward7; property last_block: UInt64 read Flast_block; property difficulty: UInt64 read Fdifficulty; property difficulty24: Float64 read Fdifficulty24; property difficulty3: Float64 read Fdifficulty3; property difficulty7: Float64 read Fdifficulty7; property nethash: UInt64 read Fnethash; property exchange_rate: Float64 read Fexchange_rate; property exchange_rate24: Float64 read Fexchange_rate24; property exchange_rate3: Float64 read Fexchange_rate3; property exchange_rate7: Float64 read Fexchange_rate7; property exchange_rate_vol: Float64 read Fexchange_rate_vol; property exchange_rate_curr: string read Fexchange_rate_curr; property market_cap: string read Fmarket_cap; property pool_fee: string read Fpool_fee; property estimated_rewards: string read Festimated_rewards; property btc_revenue: string read Fbtc_revenue; property revenue: string read Frevenue; property cost: string read Fcost; property profit: string read Fprofit; property status: string read Fstatus; property lagging: boolean read Flagging; property testing: boolean read Ftesting; property listed: boolean read Flisted; property timestamp: UInt32 read Ftimestamp; property ResponseTime: TDateTime read FResponseTime write FResponseTime; end; парсинг получается всего в одну строчку var CoinXMRPtofit := TJson.JsonToObject<TCoinProfit>(ResponseStr, [joIndentCasePreserve]); // готовый объект добавляем в Объект словарь CoinsProfitList: TObjectDictionary<String, TCoinProfit>; CoinsProfitList.AddOrSetValue(cXMR, CoinXMRPtofit);
там еще аттрибуты поддерживаются. можно писать: ['json_string'] JsonString: string и он с жсона конвернет в стандартизированный по имени объект. по сути можно изображать MVC паттерн
Обсуждают сегодня