JSON syntax is a subset of JavaScript syntax.

The JavaScript function JSON.parse(text) can be used to convert a JSON text into a JavaScript object:

var obj = JSON.parse(text);
Advertisements

What is JSON and why would I use it?

JSON: JavaScript Object Notation.

JSON is syntax for storing and exchanging text information. Much like XML.

JSON is smaller than XML, and faster and easier to parse.

JSON Example

{
“employees”: [
{ “firstName”:”John” , “lastName”:”Doe” },
{ “firstName”:”Anna” , “lastName”:”Smith” },
{ “firstName”:”Peter” , “lastName”:”Jones” }
]
}

What is JSON?

  • JSON stands for JavaScript Object Notation
  • JSON is lightweight text-data interchange format
  • JSON is language independent *
  • JSON is “self-describing” and easy to understand

JSON uses JavaScript syntax for describing data objects, but JSON is still language and platform independent. JSON parsers and JSON libraries exists for many different programming languages.

JSON – Evaluates to JavaScript Objects

The JSON text format is syntactically identical to the code for creating JavaScript objects.

Because of this similarity, instead of using a parser, a JavaScript program can use the built-in eval() function and execute JSON data to produce native JavaScript objects.

How to create an array of objects??

<?php

$array = array();
$data = new stdClass();

$data->id = ‘id’;
$data->name = ‘name’;
$data->color = ‘color’;

$array[] = $data;
$array[] = $data;
$array[] = $data;

print_r($array);

?>

___________________________________________________________________________________

class myc
{
var $i='dude';
function pr(){echo 'hi '.$this->i.'<br>';}
}
$test = new myc;
$test->i=’compadre’;
$testtwo = new myc;
$testtwo->i = ‘senor’;

$arr = array($test, $testtwo);

$arr[0]->pr();
$arr[1]->pr();

foreach($arr AS $cl) { $cl->pr(); }