Live chat Whatsapp
Live chat Whatsapp
Attiva Web - Realizzazione siti internet, ecommerce

Read and manage JSON data with Javascript

    • With asynchronous calls made through ajax, the need to read data in JSON format may arise.

      In some cases we could already take advantage of an explicit declaration of the type of incoming data, but sometimes this is not possible, to get around this fortunately the Javascript developers have created a native function, we see an example:

      var json = '{"result": "ok", "data": "my data also in HTML format"}';
      obj = JSON.parse (json);

      if I try to read the json variable as it is, I would get exactly the one shown as output, if instead I use the parsing function I can manage the contents of that block variable, for example I could extract only the desired value, to follow the complete example :

      var json = '{"result": "ok", "data": "my data also in HTML format"}';
      obj = JSON.parse (json);
      
      alert (json ['success']); // I get an 'ok' value with an alert
      
      alert (json ['data']); // I get the value in 'data' with an alert

       

      Translate with Google Traslator
       

      Francesco // 03-01-2019 // Javascript

    Lascia un commento

    Invia commento


    ATTENZIONE: il tuo commento verrà prima moderato e se ritenuto idoneo sarà pubblicato