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

To add comments, please log in or register
Erwann Pannerec
281
Erwann Pannerec  

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.

Erwann Pannerec
281
Erwann Pannerec  

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.

markusb79
23
markusb79  
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());

12
To add comments, please log in or register