Simplehost Web Hosting

Basic JSON theory for JavaScript developers

JSON stands for JavaScript Object Notation. And basically looks something like this

	  {     "firstName": "John",     "lastName": "Smith",     "age": 25,     "address": {         "streetAddress": "21 2nd Street",         "city": "New York",         "state": "NY",         "postalCode": "10021"     },     "phoneNumber": [         {             "type": "home",             "number": "212 555-1234"         },         {             "type": "fax",             "number": "646 555-4567"         }     ] }

This is an alternate format to serialising data and its key benefit is that it can be converted to an object and back to a string again for data transmission in almost any language.

It is easy to inspect as it has a structure, and is a native JavaScript syntax so a simple eval() function can bring it back to life. It is common to use this instead of XML for many server requests, such as AJAX or API services.

jQuery's AJAX methods automatically convert a returned json result into an object so you can access its properties without any hassle. e.g.

	  $.get(this.href,{},function(json_response){<br>
	    alert(json_response.firstName + ' ' + json_response.lastName);<br>
	  });

Note how json_response is just a regular object like any other.

On the server side, we can use PHP like this to return a JSON result.

	  <?php
	  class foo{
	    public $firstName;
	    public $lastName;
	  }
	  $obj_foo = new foo();
	  $json_response = json_encode($obj_foo);
	  header('Content-type: application/json');
	  print $json_response;
	  ?>

Please note however, that IE still does not recognise the application/json mime type, but you can still send it back as text/plain or load a JSON parser library for IE.

This should be enough to get you started on your way. For more information you may wish to look at these pages:

- PHP JSON
- Wikipedia JSON
- jQuery JSON / AJAX

Recent News
Feb 13, 2016

We have deployed PHP 7.0 to selected servers which has up to 4x performance gain on previous versions as well as the most comprehensive language API available. Read More


Feb 08, 2015

Bitcoin payments are now accepted for all hosting products. Bitcoin payments are simple, fast and simple to do on a mobile device. Get your digital wallet today and pay with Bitcoin. Read More