I would need to get data from inside a JSON Array.
I use a call to WebRequest()
function to get an order list from my db, thru a nodejs(api) and then I got the following JSON-data format e.g:
[{"orderid": 123556,
"ordertype": 0,
"ordercurrency": "EURUSD",
"orderdt": "2016-12-03 03:00:00"
},
{"orderid": 123457,
"ordertype": 0,
"ordercurrency": "GBPUSD",
"orderdt": "2016-12-03 03:15:00"
}
]
Any idea how to transform it to a normal array in order to manage data?
Thank you.
/Koul
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!
来源:https://stackoverflow.com/questions/41360826/managing-json-array-format-in-mql4-script