json from webserver to array in mql4

To add comments, please log in or register
wisdomcalls
8
wisdomcalls  

Please help.

I am trying to get this into an array I can work with in mql4.
No luck so far.


[{"orderid":291,"symbol":"GBPNZD","type":"sellstop","price":"1.72","sl":"1.7320","tp":"1.682","ts":100,"expiry":"0000-00-00","status":"pending"},{"orderid":292,"symbol":"GBPNZD","type":"buylimit","price":"1.671","sl":"1.6590","tp":"1.72","ts":100,"expiry":"0000-00-00","status":"pending"},{"orderid":293,"symbol":"GBPNZD","type":"sellimit","price":"1.77230","sl":"1.77310","tp":"1.7546","ts":50,"expiry":"0000-00-00","status":"pending"},{"orderid":294,"symbol":"EURCAD","type":"sellimit","price":"1.5565","sl":"1.5645","tp":"1.5417","ts":50,"expiry":"0000-00-00","status":"pending"}]
Marco vd Heijden
Moderator
12735
Marco vd Heijden  
Search - MQL5.community
Search - MQL5.community
  • www.mql5.com
Searching is based on morphology and is insensitive to case. All letters, no matter of their case, will be processed as lowercase. By default, our search engine shows pages, that...
nicholi shen
2392
nicholi shen  
wisdomcalls:

Please help.

I am trying to get this into an array I can work with in mql4.
No luck so far.


Hi wisdomcalls,

This is actually quite easy when you use the JAson library found here. Here is a script to show you exactly how it's done. Good luck!

//+------------------------------------------------------------------+
//|                                           JsonToArrayExample.mq4 |
//|                                                      nicholishen |
//|                         https://www.forexfactory.com/nicholishen |
//+------------------------------------------------------------------+
#property copyright "nicholishen"
#property link      "https://www.forexfactory.com/nicholishen"
#property version   "1.00"
#property strict
#include <JAson.mqh>
#include <Arrays\ArrayObj.mqh>
//+------------------------------------------------------------------+
string json_string = "[{\"orderid\":291,\"symbol\":\"GBPNZD\",\"type\":\"sellstop\",\"price\":\"1.72\",\"sl\":\"1.7320\",\"tp\":\"1.682\",\"ts\":100,\"expiry\":\"0000-00-00\",\"status\":\"pending\"},{\"orderid\":292,\"symbol\":\"GBPNZD\",\"type\":\"buylimit\",\"price\":\"1.671\",\"sl\":\"1.6590\",\"tp\":\"1.72\",\"ts\":100,\"expiry\":\"0000-00-00\",\"status\":\"pending\"}]";
//+------------------------------------------------------------------+
class Order : public CObject
{
public:
   string   symbol;
   int      id;
   string   type;
   string to_string(){
      return StringFormat("%s order. ID=%d, TYPE=%s", symbol, id, type);
   }
};
//+------------------------------------------------------------------+
class OrderVector : public CArrayObj
{
   public: Order *operator[](int i){return this.At(i);}
};
//+------------------------------------------------------------------+
void OnStart()
{
   OrderVector orders;
   CJAVal json;
   if(json.Deserialize(json_string)){
      for(int i = json.Size()-1; i>=0; i--){
         Order *order = new Order();
         order.symbol = json[i]["symbol"].ToStr();
         order.id = (int)json[i]["orderid"].ToInt();
         order.type = json[i]["type"].ToStr();
         orders.Add(order);
      } 
   }
   for(int i=0; i < orders.Total(); ++i)
      Print(orders[i].to_string());
}
//+------------------------------------------------------------------+

output:

JsonToArrayExample EURUSD,M5: GBPNZD order. ID=292, TYPE=buylimit
JsonToArrayExample EURUSD,M5: GBPNZD order. ID=291, TYPE=sellstop
To add comments, please log in or register