JavaScript - Parsing JSON Object Using AngularJS

Submitted by: 
Language: 
Visitors have accessed this post 104 times.

In this tutorial we will create a Parsing JSON Object Using AngularJS. This code will automatically populate the HTML table when the user click the parsing button. The code itself use a simple AngularJS directive ng-click to initiate a method that handle in retrieving of JSON file using $http.get() by providing the full file name of the JSON object. This is a free source code you can modify it and use it on your working programs.

AngularJS is a JavaScript-based open-source front-end web application framework . It is a kind of template that extends HTML to a new level of coding techniques. It is mostly used by other well known site for creating a template.

Getting started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Lastly, you will need to download the AngularJS here's the link https://angularjs.org/.

Note: This code will only work if run in a local server.

Creating the Main Interface

This code contains the interface of the application. This code will render application and display the form. To do that just kindly write these block of code inside the text editor and save this as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en" ng-app="myModule">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body ng-controller="myController">
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">JavaScript - Parsing JSON Object Using AngularJS</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18.  
  19. <div class="col-md-3">
  20. <label>JSON file name</label>
  21.  
  22. <center><h4><i>user.json</i></h4></center>
  23.  
  24. <br />
  25. <center><button ng-click="getData()" class="btn btn-primary btn-sm">Parse JSON Object</button></center>
  26. </div>
  27. <div class="col-md-9">
  28. <table class="table table-bordered">
  29. <thead class="alert-info">
  30. <tr>
  31. <th>Firstname</th>
  32. <th>Lastname</th>
  33. <th>Username</th>
  34. <th>Password</th>
  35. </tr>
  36. </thead>
  37. <tr ng-repeat="user in users">
  38. <td>{{user.firstname}}</td>
  39. <td>{{user.lastname}}</td>
  40. <td>{{user.username}}</td>
  41. <td>{{user.password}}</td>
  42. </tr>
  43. </tbody>
  44. </table>
  45. </div>
  46. </div>
  47. <script src="js/angular.js"></script>
  48. <script src="js/script.js"></script>
  49. </body>
  50. </html>

Creating the Script

This code contains the main function of the application. This code will display the data from a JSON file. To that just kindly copy and write these block of codes inside the text editor, then save it as shown below.

user.json

  1. [
  2. {"firstname": "John", "lastname": "Smith", "username": "john123", "password": "john33333"},
  3. {"firstname": "Claire", "lastname": "Temple", "username": "claire", "password": "clairetemp123"}
  4. ]

script.js
Note: Save this file inside the js directory as script.js to make the program work.

  1. var app = angular.module('myModule', [])
  2. .controller('myController', function($scope, $http){
  3.  
  4.  
  5. $scope.getData = function(){
  6. $http.get('user.json').then(function(response){
  7. $scope.users = response.data;
  8. });
  9. }
  10. });

There you have it we successfully created a Parsing JSON Object Using AngularJS. I hope that this very simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.