In this article we will show you the solution of jQuery parse json array, here we are going to show you example to achieve result with the help of each() and html() methods.
The each() method used to iterate json array then printed particular property value on browser.
The html() method used to append retrieved json array property result on webpage as well as displayed on console by console.log() method.
Step By Step Guide On jQuery Parse JSON Array :-
Here we defined html block with button ‘Display’ which is help you to load major part of code on browser and h4 tag used to display parsed json array value.
In script block we defined ready() method which is automatically loads program when open browser within that click() function defined to load main process after user clicks ‘Display’ button.
There we specified json array ‘jsarr’ with three property name and values. The ‘jsarr’ needs to append with each method to iterates and display specific property values on browser.
<!DOCTYPE html> <html> <head> <title>PARSE JSON</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script> $(document).ready(function(){ $(".btn").click(function(){ var jsarr=[{"Name":"Prawin","Age":25,"City":"Kangayam"}]; $(jsarr).each(function(ind,item){ console.log(item.Name); $("h4").html(item.Name); }) }); }) </script> </head> <body> <button class="btn">Display</button> <h4></h4> </body> </html>
- <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
- The<html> tag is used to indicate the beginning of HTML document.
- As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
- Here we imported open source jquery library support file which is needed for coding using jquery in program.
- Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- In script we defined ready() function then click() method defined with button by specifying attribute class name ‘btn’.
- Within that we defined json array ‘jsarr’ with three properties ‘{"Name":"Prawin","Age":25,"City":"Kangayam"}’. To parse json array we specified ‘jsarr’ with each() loop.
- Now you can iterates json array values, in each loop ‘ind-pointing index value, item-pointing iteration value’. There we displaying first property ‘Name’ value by appending variable ‘item’ with array property ‘Name’.
- In html block we defined button element ‘Display’ with attribute ‘class’ respective value ‘btn’ and h4 for display final result.
- Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.
Conclusion :-
In conclusion now we are able to know how to parse json array using jquery.
Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes in document without any error.
When we executes this program on browser we can see button ‘Display’ now user needs to click on it then we can see json array Name property value ‘Prawin’ on webpage.
If you wants to print ‘Age or City’ property value then change ‘Name’ value to wanted property name accordingly.
I hope this article on jQuery parse json array helps you and the steps and method mentioned above are easy to follow and implement.