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

 

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

 

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

Thanks in advance

 

I have received the following string from an http request:

{"records":[{"id":"rec4haaOncoQniu8U","fields":{"orders1":5},"createdTime":"2020-02-08T09:08:22.000Z"}]}

I am not understanding how I can process and separate the values of the json in mql4 using the "JAson.mqh " library, located here: https://www.mql5.com/en/code/13663

I need the values of "orders1" located under "fields" ,   value = 5.

the only "KEYS" that changes are the keys within the "fields" values, all other keys are constants.

i would like to be able to get the values with something like this:

string value1 = Result[0].["fields"].["orders1"]; //5

string value2 = Result[0].["fields"].["orders2"];

Please let me know what I can do.

Thanks

 
Eli Mizrahi:

Do not double post!

Your other post has been deleted.

 

I am using the Telegram api and then deseriazle the text. My problem is that there is a carriage return inside the json after "update_id" structure and the deserialise stops with no error


{"ok":true,"result":[{"update_id":568022205,
"channel_post":{"message_id":434,"chat":{"id":-1001436032340,"title":"FORTUNA","type":"channel"},"date":1588890767,"reply_to_message":{"message_id":298,"chat":{"id":-1001436032340,"title":"FORTUNA","type":"channel"},"date":1588753581,"text":"text","entities":[{"offset":67,"length":11,"type":"mention"}]},"text":"this is the text"}}]}

My code is 

string out;
      string url=StringFormat("%s/bot%s/getUpdates",TELEGRAM_BASE_URL,m_token);
      string params=StringFormat("offset=%d",m_update_id);
      //---
      int res=PostRequest(out,url,params,WEB_TIMEOUT);
      if(res==0)
        {
        
         Print(StringDecode(out));
         //--- parse result
         CJAVal js(NULL,jtUNDEF);
         bool done=js.Deserialize(out);
         if(!done)
            return(ERR_JSON_PARSING);

         bool ok=js["ok"].ToBool();
         if(!ok)
            return(ERR_JSON_NOT_OK);
 

hi  this error :


array out of range in 'jAson.mqh' (330,22)  // this error


position:


case '{': // начало объекта. создаем объект и забираем его из js

i0=i+1;

if (m_type!=jtUNDEF) { Print(m_key+" "+string(__LINE__)); return false; }// ошибка типа

m_type=jtOBJ; // задали тип значения

i++; if (!Deserialize(js, slen, i)) { Print(m_key+" "+string(__LINE__)); return false; } // вытягиваем его

return js[i]=='}' || js[i]==0;   // this error  what about are me ?

break;



2823500551@qq.com

 

Hi

Would you please provide me a code to parse this json url?

https://search.codal.ir/api/search/v2/q?&Audited=true&AuditorRef=-1&Category=-1&Childs=true&CompanyState=-1&CompanyType=-1&Consolidatable=true&IsNotAudited=false&Length=-1&LetterType=-1&Mains=true&NotAudited=true&NotConsolidatable=true&PageNumber=1&Publisher=false&TracingNo=-1&search=true;
 
Thank you for a great product! It works well while there is little data, but if the array is more than 1000 rows, the deserialization does not work. Please tell me how to fix it.
Reason: