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

 
JAval j1; j1.Add(1);

JAval j2; j2.Add(2); j2.Add(3);

JAval js;

js["a"].Add(j1);
js["a"].Add(j2);


// on the exhaust - { a: [ [1], [2,3] ]; }
 
o_o:

It seems clear, but I can't understand something... For example, a simple practical task: we copy the charts and make an array of arrays with 3 charts in each, with the fields "symbol" and"period".

That is, as a result we should get the following: { "keyboard": [ [ [{"symbol":USDJPY, "period":5},{"symbol":AUDUSD, "period":5},{"symbol":EURUSD, "period":5}], [{"symbol":GBPUSD, "period":5},{"symbol":NZDUSD, "period":5},{"symbol":EURJPY, "period":5}] } ] }

The number of elements in the nested array can be configured via a variable.

 
JAval ja, je, js;

je["symbol"]="USDJPY"; je["period"]=5; ja.Add(je);
je["symbol"]="AUDUSD"; je["period"]=5; ja.Add(je);
je["symbol"]="EURUSD"; je["period"]=5; ja.Add(je);

js["keyboard"].Add(ja);

ja.Clear();
je["symbol"]="GBPUSD"; je["period"]=5; ja.Add(je);
je["symbol"]="NZDUSD"; je["period"]=5; ja.Add(je);
je["symbol"]="EURJPY"; je["period"]=5; ja.Add(je);

js["keyboard"].Add(ja);


Are you really unclear or just too lazy to code yourself? )

 
o_o:


Are you really unclear or just too lazy to code yourself? )


No, Alexey, I am not too lazy to code. The point is different: all your examples have a fixed number of array items. Here is my code, which unfortunately does not work. I can't understand what the error is. (((

   CJAVal j_keyboard; // Main array
   CJAVal j_array;    // Nested array 
   CJAVal j_item;     // Array element 

   //--- Determine the number of transmitted elements
   int count = ArraySize(array_value);
   if(count == 0) return(j_keyboard.Serialize()); // No elements

   int col = 0;
   for(int item = 0; item<count; item++)
      {
         //--- Filling the simplest element
         j_item["text"] = array_value[item];
         
         if(col<columns) // If the number is less than the specified columns, add to the array
            {
               j_array.Add(j_item);
            }
         else
            {   
               
               // Reach the specified number of columns, add them to the main array
               j_keyboard["keyboard"].Add(j_array);
               col = 0;
               j_array.Clear(); // Clear nested array
            }
      }
   
   Print(j_keyboard.Serialize());
array_value - Это строчный массив переданных элементов
 
Алексей Барбашин:

Here's my code, which unfortunately doesn't work.

how did you realise it didn't work?

 
o_o:

how did you realise it wasn't working?


The serialisation string outputs an empty object. I'm going to make a script and upload it here.

.... after a while...

Alexei, I'm sorry, I screwed up... I didn't add the iterator increment... (((

Everything works. That's what it means just to chat with an intelligent person! ))) Thanks again for your development, which I have been using for almost a year now!

Files:
 
Алексей Барбашин:

The serialisation string outputs an empty object. I'll throw a script together and post it here.

It works for me.

#include <JAson.mqh>
//------------------------------------------------------------------ OnStart
void OnStart()
{
        string array_value[]={"000", "111", "012", "AAA", "BBB", "ABC"};
        Print(Get(array_value, 3));
}
//------------------------------------------------------------------ Get
string Get(string& array_value[], int columns)
{       
        CJAVal jk; // Main array
        int n=ArraySize(array_value); if (n==0) return jk.Serialize(); // No elements
        
        CJAVal ja;    // Nested array 
        for(int i=0, c=0; i<n; i++, c++)
        {
                if (c>=columns) { jk["keyboard"].Add(ja); c=0; ja.Clear(); }
                CJAVal ji; ji["text"]=array_value[i]; ja.Add(ji); // add
        }
        if (ArraySize(ja.m_e)>0) jk["keyboard"].Add(ja);
        
        return jk.Serialize();
}

{"keyboard":[[{"text":"000"},{"text":"111"},{"text":"012"}],[{"text":"AAA"},{"text":"BBB"},{"text":"ABC"}]]}


What's wrong with your code?

 
o_o:

It works for me.


What's wrong with your code?


Answered above. Found my mistake. Thank you! )))

 

Алексей Барбашин:

It's working. That's what it means to just chat with an intelligent person! )))

there is a debug for everything. it is much smarter ))

Thanks again for your development which I have been using for almost a year now!

you are welcome

 

the WebRequest result is an array. and i invoke like this

jv.Deserialize( result);


how can i iterate it