JSON (JavaScript Object Notation) is a lightweight syntax for storing and exchanging text information.
In this lesson you can learn how to create simple and multidimensional arrays and objects in JavaScript using JSON format.
Although the JSON syntax contains a minimum set of rules for structuring data in text format, JSON can represent complex data structure that can contain arrays and objects into a string that can be interchanged and used between different programming languages.
The data in JSON format are stored using name:value
pairs, separated by comma. The 'name' can be a string between quotes, or a number, if it is not specified, the 'name' will be a numerical index, like in numeric arrays.
- JSON can represent these data types: strings, numbers, boolean, null, array and objects.• Because JSON syntax is a subset of JavaScript syntax, the elements of the arrays and objects created using JSON format can be accessed, and edited directly with JavaScript functions.
- Arrays are created by adding the list of values (separated by comma) between square brackets '[]'.
- Objects are created by adding"name":"value"
pairs, separated by comma, between curly brackets '{}'.
- The 'name', and 'value' which are text string must be added between simple or double quotes, with ':' between 'name' and 'value'.
An ordered list of values, separated by comma, between square brackets, [].
- Example, the numeric values are not added between quotes.// Numeric array in javascript syntax var arr1 = new Array(); arr1[0] = 'val 1'; arr1[1] = 2; arr1[2] = 'val 3'; // the same array, created with JSON: var jsnar =['val 1', 2, 'val 3']; // the elements stored in 'jsnar' can be accessed like any array element document.write(jsnar[0]); // val 1 // modify the value of the second element in 'jsnar' jsnar[1] = 7.8; document.write('<br>'+ jsnar[1]); // 7.8
// Object with JavaScript syntax var siteData = new Object(); siteData.url = 'https://coursesweb.net/'; siteData.title = 'Web Development Courses'; siteData.users = 1500; // the same object, in JSON format var jsnob = {'url': 'https://coursesweb.net/', 'title': 'Web Development Courses', 'users': 1500}; // accessing the 'title' element in 'jsnob' document.write(jsnob.title); // Web Development Courses // Or: document.write('<br>'+ jsnob['title']); // Web Development Courses // adding another entry in 'jsnob' jsnob.new_item = 'new entry'; document.write('<br>'+ jsnob['new_item']); // new entry (same with: alert(jsnob.new_item) )
// Multidimensional arrays in JSON format var arr2 = [ ['php', 'html', 'javascript', 78], ['free courses', 'tutorials'], ['marplo.net', 'coursesweb.net', 'www.w3schools.com', 2012] ]; // accessing the first element in the second array document.write(arr2[1][0]); // free courses // modify the value of the first entry in the second array, in 'arr2' arr2[1][0] = 'json tutorial';
// Multidimensional object in JSON format var jsnobj = { 'site': {'url': 'https://coursesweb.net/', 'title': 'Web Development Courses', 'users': 1500}, 'page': {'course': 'javascript-jquery', 'lessons': 35}, 'names': {'name_1': 'Marius', 'name_2': 'Victor', 'name_3': 'Alex'} } // accessing the 'course' item in the object 'page' document.write(jsnobj.page.course); // javascript-jquery // Or: document.write('<br>'+ jsnobj['page']['course']); // javascript-jquery // modify the value of the 'name_1', in the object in 'names' jsnobj.names.name_1 = 'MarPlo';
// array with 2 objects var arrob = [ {'url': 'https://coursesweb.net/', 'title': 'Web Development Courses', 'users': 1500}, {'url': 'https://marplo.net/', 'title': 'Free Courses, Games, Anime', 'users': 4500} ] // accessing 'users' intem of the second element in 'arrob' document.write(arrob[1].users); // 4500 // Or: document.write('<br>'+ arrob[1]['users']); // 4500
// JSON object with 2 arrays and an object var obj2 = { 'courses': ['html', 'css', 'ajax'], 'tutorials': ['jquery', 'actionscript'], 'site': {'url': 'https://coursesweb.net/', 'title': 'Web Development Courses', 'users': 1500} } // accessing the first element in 'tutorials' document.write(obj2.tutorials[0]); // jquery // Or: document.write('<br>'+ obj2['tutorials'][0]); // jquery
// JSON object with one property (values), and a method, getVal() var objsn = { 'values': [2, 7, 8], 'getVal': function(nr) { // multiply the value of the 3rd element in the 'values' array by number in parameter var reval = this.values[2] * nr; return reval; // returns the result } }; var test = objsn.getVal(9); // calls the getVal() method document.write(test); // 72
The this
word used in the getVal() function represents the object in which the function is used. So, the this.values[2]
returns the third value added in the 'values' property of the current object ( objsn ).
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net