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

 
danielsokolowsk:

Would anyone or the author answer if this is supposed to support nested JSON? That is a CJAVal instance contains a keys '2', and '3' which are another CJAVal instance. Code seems to run but when serialized the keys are blank.

Example, I am getting:


But should be getting 

https://www.mql5.com/ru/forum/63015

a few examples

   string a[] = {"1", "2", "3"};
   int b[] = {1, 2, 3};
   
   CJAVal js;
   js["a"].Add(a[0]);
   js["a"].Add(a[1]);
   js["a"].Add(a[2]);
        
   js["b"].Add(b[0]);
   js["b"].Add(b[1]);
   js["b"].Add(b[2]);
   
   js["c"][0]=1.1;
   js["c"][1]=2.9;
   js["c"][2]=3.03;
   
   string t=js.Serialize();
   Print(t);   // {"a":["1","2","3"],"b":[1,2,3],"c":[1.10000000,2.90000000,3.03000000]}   
   
   
   js.Clear();
   js.Deserialize(t); 
   Print(js["c"][2].ToStr()); //  3.03000000

array of arrays

JAval j1; j1.Add(1);

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

JAval js;

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

// { a: [ [1], [2,3] ]; }
Библиотеки: JSON Serialization and Deserialization (native MQL)
Библиотеки: JSON Serialization and Deserialization (native MQL)
  • 2015.08.18
  • www.mql5.com
Статьи и техническая библиотека по автоматическому трейдингу: Библиотеки: JSON Serialization and Deserialization (native MQL)
 
Great design, thanks bro! Works like clockwork, used it in my robot.
 
Konstantin Efremov:
Great design, thanks bro! Works like clockwork, used it in my robot.

There are some bugs in the bible. attached is the corrected version, thanks to the author and probably won't mind.

Files:
json.mqh  44 kb
 

Hello,


I try to use this library to read a json file.


could you provide an exemple in a such case ?


My first lines are :

   string s[];
   int cpt=0;
   
   int handle=FileOpen("json_sample.txt",FILE_READ|FILE_TXT|FILE_ANSI);
   while(!FileIsEnding(handle)) {ArrayResize(s,ArraySize(s)+1); s[cpt++]=FileReadString(handle);}
   FileClose(handle);


What have I to do in order to store somes values from json file in my variables ?


Thanks for the help !

Erwann.

 

After somes researchs I solve my problem :)


It's not necessary to use string table [] because of limitation of string is 256 bytes.


There is no string limitation length in fact (only for initialisation between quotes "".

So you just have to use this code :

   CJAVal srce;
   
   string s;
   int cpt=0;
   
   int handle=FileOpen("json_sample.txt",FILE_READ|FILE_TXT|FILE_ANSI);
   while(!FileIsEnding(handle)) StringAdd(s,FileReadString(handle));
   FileClose(handle);
   
   srce.Deserialize(s);


the s string variable could have a length more than 256 charachters :)


Bye,

Erwann.

 
danielsokolowsk:

Would anyone or the author answer if this is supposed to support nested JSON? That is a CJAVal instance contains a keys '2', and '3' which are another CJAVal instance. Code seems to run but when serialized the keys are blank.

Example, I am getting:


But should be getting 

Daniel, I ran into the issue with empty keys as well, when using nested JSON objects (not arrays). I resolved it by using the Set() function. Example:

CJAVal msg, content;
msg["messageName"] = "TickData";
content["instrument"] = Symbol();
content["timeFrame"] = (int) Period();
content["time"] = (int) TimeCurrent();
content["open"] = iOpen(Symbol(), Period(), 0);
msg["content"].Set(content);
Print("Sending JSON to server: ", msg.Serialize());

 
Andrey Dik:

there are some bugs in the bible. attached is the corrected version, thanks to the author and probably won't mind.

thanks!

 

How do I remove an element from a JSON tree. There is method to Set, to Add but not to Delete

Thanks in advance

 

awesome,I have added ulong and datetime type support to it.

 
Ting Yu:

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

jv.Deserialize( result);


how can i iterate it

if result likes
[{"a":1},{"a":2}]

'

you can use jv[0]["a"].ToInt() to get 1,jv[1]["a"].ToInt() to get 2