Parse json reading data!

To add comments, please log in or register
sostrader
16
sostrader  

Any one could help me how can i parse this data to my mql4

 {"respond":1,"paging":{"stillmore":0,"perpage":0,"callpage":0,"next":0,"previous":0,"pages":0,"result":0},"message":"","result":[{"user_id":"243","enddate":"2018-02-20 00:00:00","user_email":"teste@test.com","display_name":"Jonh Tester","status":"active"}]}



im only getting the respond = 1....  if i print resp i can get all data as json  on mt4 log but when i try to get for example only display_name from json to comment on the chart its comes nothing


im using json.mqh and hash.mqh for http request im using wininet.dll base and working  nice and its very fast response


string resp = httpGET("https://"+ip_adress+""+prefixApi);
  
   JSONParser *parser = new JSONParser();

    JSONValue *jv = parser.parse(resp);

    if (jv == NULL) {
        Print("error:"+(string)parser.getErrorCode()+parser.getErrorMessage());
    } else {


        Print("PARSED:"+jv.toString());
        
        if (jv.isObject()) { // check root value is an object. (it can be an array)

            JSONObject *jo = jv;

            // Direct access - will throw null pointer if wrong getter used.
           // Print("firstName:" + jo.getString("firstName"));
           // Print("city:" + jo.getObject("address").getString("city"));
            //Print("phone:" + jo.getArray("message").getObject(0).getString("number"));

             Safe access in case JSON data is missing or different.
             if (jo.getString("user_mail",resp) ) Print("User = "+resp);

            // Loop over object keys
            JSONIterator *it = new JSONIterator(jo);
            for( ; it.hasNext() ; it.next()) {
                Print("loop:"+it.key()+" = "+it.val().toString());
            }
            delete it;
        }
        delete jv;
    }
    delete parser;


Francis Dube
53344
Francis Dube  

Hello, sorry for the late reply.

If you are trying to access specific members of the object you have to take into consideration the structure of the object.

For example to retrieve the "user_mail" :

  this data member is encapsulated in the Json array "result" . So you first have to retrieve the array

JSONObject *jo = jv;
JSONArray  *jd =  jo.getArray("result");

//jd json array now contains the members of the array , which is one object, so you can access the members of this
// object as follows

Print("User Email - "+jd.getObject(0).getString("user_email"));

sostrader
16
sostrader  
Francis Dube :

Hello, sorry for the late reply.

If you are trying to access specific members of the object you have to take into consideration the structure of the object.

For example to retrieve the "user_mail" :

  this data member is encapsulated in the Json array "result" . So you first have to retrieve the array

Thank you, friend, that's exactly what I did.


my problem was exactly that I did not know which array was the key to fetching the rest

MemberMail=("" + jo.getArray("result").getObject(0).getString("user_email"));


// Print to test call function

        Print("Member:", MemberMail);
To add comments, please log in or register