JavaScript JSON parse array | Example code

  • by

Javascript has a built-in JSON parse for strings, which you have to use loop through it for JSON parse array. using a for-in loop will set the var to be the property name of the current loop, not the actual data.

var myObject = JSON.parse("my json string");

JavaScript JSON parse array example

Simple example code.

<!DOCTYPE html>
<html>
<body>

  <script>
    var msg = '{"success": true,"counters": [{"counter_name": "dsd", "counter_type": "sds", "counter_unit": "sds" }, { "counter_name": "gdg", "counter_type": "dfd", "counter_unit": "ds" }, { "counter_name": "sdsData", "counter_type": "sds", "counter_unit": " dd " }, { "counter_name": "Stoc final", "counter_type": "number ", "counter_unit": "litri " }, { "counter_name": "Consum GPL", "counter_type": "number ", "counter_unit": "litri " } ] }';

    var jsonData = JSON.parse(msg);
    for (var i = 0; i < jsonData.counters.length; i++) {
      var counter = jsonData.counters[i];
      console.log(counter.counter_name);
    }
  </script>

</body>
</html>

Output:

JavaScript JSON parse array

Source: stackoverflow.com

Do comment if you have any doubts or suggestions on this JS JSON array topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.