Managing JSON Array format in MQL4 script

谁都会走 提交于 2019-12-06 14:28:19

JSON Serialization and Deserialization library works pretty well. You can include it and finish your task within a minute or get an inspiration from its code.

https://www.mql5.com/en/code/13663

https://www.mql5.com/en/forum/65320

Let's assume you have the JSON data you pasted in a string variable named data.

#include <JAson.mqh>

// Object
CJAVal json;

// Load in and deserialize the data
json.Deserialize(data);

// Try to access the data elements
Alert(json["orderid"].ToInt());
Alert(json["ordercurrency"].ToStr());

How?
Easy:

JSON-format is a "string-ified" representation of data-elements, put into a common container, be it a uchar[] or a string on the MQL4 side.

So, let's create a JSON-parser, coherent with the subset of the standard JSON-format syntax rules.

1) Search for all db-output "row"-s ( encapsulated in {...} )

2) Decode all db-output "column"-s ( encoded in ( known ) "KEY":VALUE pairs )

3) Store decoded values into any kind of target representation,

int    orderIdNoARRAY[];
int    orderTypeARRAY[];
string orderCurrARRAY[];
string orderDateARRAY[];
int    anArrayStackPTR = 0
...
orderIdNoARRAY[anArrayStackPTR] = aDecodedOrderID;    // 123556
orderTypeARRAY[anArrayStackPTR] = aDecodedOrderTYPE;  // 0
orderCurrARRAY[anArrayStackPTR] = aDecodedOrderCURR;  // "EURUSD"
orderDateARRAY[anArrayStackPTR] = aDecodedOrderDATE;  // "2016-12-03 03:00:00"

or

#define oID   0
#define oTYPE 1
#define oCCY  2
#define oDATE 3

string stringDataFromJSON[10000,4];
int    anArrayStackPTR = 0
...
stringDataFromJSON[anArrayStackPTR,oID]   = (string)aDecodedOrderID;      // 123556
stringDataFromJSON[anArrayStackPTR,oTYPE] = (string)aDecodedOrderTYPE;    // 0
stringDataFromJSON[anArrayStackPTR,oCCY]  = (string)aDecodedOrderCURR;    // "EURUSD"
stringDataFromJSON[anArrayStackPTR,oDATE] = (string)aDecodedOrderDATE;    // "2016-12-03 03:00:00"

or

struct        aDB_RECORD{
       int    aDB_oID;
       int    aDB_oTYPE;
       string aDB_oCCY;
       string aDB_oDATE;
};

aDB_RECORD anArrayOfSTRUCTs[];
int    anArrayStackPTR = 0
...
anArrayOfSTRUCTs[anArrayStackPTR].aDB_oID   = aDecodedOrderID;    // 123556
anArrayOfSTRUCTs[anArrayStackPTR].aDB_oTYPE = aDecodedOrderTYPE;  // 0
anArrayOfSTRUCTs[anArrayStackPTR].aDB_oCCY  = aDecodedOrderCCY;   // "EURUSD"
anArrayOfSTRUCTs[anArrayStackPTR].aDB_oDATE = aDecodedOrderDATE;  // "2016-12-03 03:00:00"

Yes,
it is that easy!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!