Libraries: JSON Serialization and Deserialization (native MQL) - page 10

 

fixed

1.13

Files:
JAson.mqh  31 kb
 
o_o:
Yeah, I probably didn't understand what your problem was, the lines you showed you were able to get everything.
{
  "server_time": 1519120845,
  "pairs": {
    "btc_usd": {
      "decimal_places": 3,
      "min_price": 0.1,
      "max_price": 500000,
      "min_amount": 0.001,
      "hidden": 0,
      "fee": 0.2
    },
    "btc_rur": {
      ...
    },
    "btc_eur": {
      ...

The exchange gives this response.... (and the number of pairs can be different... for example, if the exchange will switch off or add some pairs).

I need to somehow get all the names of pairs in the response (btc_usd, btc_rur, btc_eur, etc.) in a loop or through a recursive call, it doesn't matter.... I can't figure out how to get the first and next name of a pair... I can't figure out how to get the first and the next name of a pair....

 
Delta.800:

the exchange gives this response.... (and the number of pairs can be different.... for example, if the exchange will switch off or add some pair).

I need to somehow get all the names of pairs in the response (btc_usd, btc_rur, btc_eur, etc.) in a loop or through a recursive call, it doesn't matter.... I can't figure out how to get the first and next name of a pair... I can't figure out how to get the first and next names of a pair... I can't figure it out....

Cut the string at the beginning

{
  "server_time": 1519120845,
  "pairs": 

Then delete the } at the end of the string

Then feed the string to the parser.

Fill your structure with parser data. And then do what you want with your structure - names of pairs, etc.

 
Andrey Dik:

Trim the line at the very beginning

Then delete at the end of the line }

Then feed the string into the parser.

Fill your structure with parser data. And then do what you want with your structure - pair names, etc.

"Then feed the string into the parser." - is that right? dataM.Deserialize(ttt);

"Fill your structure with parser data." - how to do it? please, give me a line..... (you don't need to describe the structure))

ss I meant a simple structure, not an object oriented one, such as

struct bpInfo
{
public:
        ulong server_time;
        bpPairInfo pairs[];
        
        bool FromJson(CJAVal& ja)
        {
                server_time=(uint)ja["server_time"].ToInt();
                CJAVal* jp=ja["pairs"];
                int n=ArrayResize(pairs, ArraySize(jp.m_e));
                for (int i=0; i<n; ++i) pairs[i].FromJson(jp.m_e[i]);
                return true;
        }
};
I think there is something similar here, but I can't figure it out. (I'm not good at OOP, yet...).
 
o_o:

fixed

1.13

Try this example:

#include "JAson.mqh"

//+------------------------------------------------------------------+
void OnStart ()
{
  string in, out;
  CJAVal js (NULL, jtUNDEF);
  bool b;

  //---
  in =  "{\"фирма1\":{\"модельный_ряд1\":[[0.1,1.3],[0.2,1.5],[0.3,2.0]],\"модельный_ряд2\":[[0.1,2.3],[0.2,2.5],[0.3,3.0]]},\"фирма2\":{\"модельный_ряд1\":[[0.2,3.8],[0.3,4.0],[0.4,4.5]],\"модельный_ряд2\":[[0.2,4.8],[0.3,5.0],[0.4,6.5]]}}}";
  Print(in);
  out = "";
  b = js.Deserialize (in);
  js.Serialize (out);
  Print (out);
  
  for (int i = 0; i < ArraySize (js.m_e); i++)
  {
    Print("---");
    Print(js.m_e[i].m_key);
    
    for (int i1 = 0; i1 < ArraySize (js.m_e [i].m_e); i1++)
    {
      Print("   : ", js.m_e[i].m_e[i1].m_key);
      
      for (int i2 = 0; i2 < ArraySize (js.m_e [i].m_e [i1]. m_e); i2++)
      {
        Print("      : ", js.m_e[i].m_e[i1].m_e[i2].m_key, " :", js.m_e[i].m_e[i1].m_e[i2].ToDbl());
      }
    }
  }
}
//+------------------------------------------------------------------+
 
Delta.800:

"Then feed the string to the parser." - is that right? dataM.Deserialize(ttt);

"Fill your structure with parser data." - how to do it? please, give me a line..... (you don't need to describe the structure))

Look in my example about companies and model range - it shows how to get data from parser.....


o_o:

fixed

1.13

However..... Disassembles and collects timing perfectly, but I can't get the data.... maybe no dables, ints, etc.? - leave the string as it is in the form of string and then decide how to deal with this string from the outside.

 
Andrey Dik:

However..... Disassembles and assembles the term is great, but I can't get data..... Maybe to hell with dables, ints, etc.? - leave the string as it is in the form of string and then decide how to deal with this string from the outside.

You have an array in an array. it means you need to add another loop level i3.

модельный_ряд1 :[ [ 0.1,1.3], [ 0.2,1.5], [ 0.3,2.0] ]
#include <JAson.mqh>

//+------------------------------------------------------------------+
void OnStart ()
{
  string in, out;
  CJAVal js (NULL, jtUNDEF);
  bool b;

  //---
  in =  "{\"фирма1\":{\"модельный_ряд1\":[[0.1,1.3],[0.2,1.5],[0.3,2.0]],\"модельный_ряд2\":[[0.1,2.3],[0.2,2.5],[0.3,3.0]]},\"фирма2\":{\"модельный_ряд1\":[[0.2,3.8],[0.3,4.0],[0.4,4.5]],\"модельный_ряд2\":[[0.2,4.8],[0.3,5.0],[0.4,6.5]]}}}";
  Print(in);
  out = "";
  b = js.Deserialize (in);
  js.Serialize (out);
  Print (out);
  
  for (int i = 0; i < ArraySize (js.m_e); i++)
  {
    Print("---");
    Print(js.m_e[i].m_key);
    
    for (int i1 = 0; i1 < ArraySize (js.m_e [i].m_e); i1++)
    {
      CJAVal* j1=GetPointer(js.m_e[i].m_e[i1]);
      Print("   : ", j1.m_key);
      
      for (int i2 = 0; i2 < ArraySize (j1. m_e); i2++)
      {
        CJAVal* j2=GetPointer(j1.m_e[i2]);
        for (int i3 = 0; i3 < ArraySize (j2. m_e); i3++)
        {
           CJAVal* j3=GetPointer(j2.m_e[i3]);
           Print("         : ", j3.m_key, " :", j3.ToDbl());
        }
      }
    }
  }
}
 

A HUGE THANK YOU to everyone who responded!!!!

{
  "server_time": 1519120845,
  "pairs": {
    "btc_usd": {
      "decimal_places": 3,
      "min_price": 0.1,
      "max_price": 500000,
      "min_amount": 0.001,
      "hidden": 0,
      "fee": 0.2
    },
    "btc_rur": {
      ...
    },
    "btc_eur": {
      ...

we get the names of the couples:

CJAVal dataM;
...
dataM.Deserialize(ttt);   
Print(dataM.m_e[0].m_key);
Print(dataM.m_e[1].m_key);
for (int i1 = 0; i1 < ArraySize (dataM.m_e [1].m_e); i1++)
  {
    CJAVal* j1=GetPointer(dataM.m_e[1].m_e[i1]);
    Print("   :-", j1.m_key);
  }

Bottom line:

server_time
pairs
:-btc_usd
:-btc_rur
:-btc_eur
:-ltc_btc
...

what we needed!

 
o_o:

you have an array in an array. so you need to add another loop level i3

Sorcery, 82 levl.

Can't you make it any simpler?

 
Andrey Dik:

sorcery, 82 levl.

Couldn't you make it any simpler?

What do you mean? You're the ones who made the arrays so deep.