The Net, the Web and other Geeky stuff
To find out how JSON encoding worked with PHP, I did a little testing. PHP data can be encoded with the json_encode
function. The function does not work with objects, but seems to work quite well with arrays. As you can see with the example below, you can pretty much embed arrays to your hearts content. Click on the title link to see the output produced.
Demo: json01.php
Source: json01.php
1:<?php
2:
3:$simpleArray01 = array("One", "Two", "Three");
4:$json01 = json_encode($simpleArray01);
5:
6:$assocArray01 = array("key01" => "One", "key02" => "Two", "key03" => "Three");
7:$json02 = json_encode($assocArray01);
8:
9:$assocArray02 = array("key01" => "One", "key02" => "Two", "key03" => array("A" => "4", "B" => "5", "C" => "6"));
10:$json03 = json_encode($assocArray02);
11:
12:$assocArray03 = array("data" => array("key01" => "One", "key02" => "Two", "key03" => array("A" => "4", "B" => "5", "C" => "6")));
13:$json04 = json_encode($assocArray03);
14:
15:function myJsonData($message, $hashList){
16: return json_encode(array("message" => $message, "hash" => $hashList));
17:}
18:
19:?>
20:<html>
21:<head>
22: <title>JSON PHP Testing</title>
23:</head>
24:<body>
25: <h2>JSON PHP Testing</h2>
26:
27: <p>A Simple Array</p>
28: <pre>
29:<? echo $json01; ?>
30: </pre>
31:
32: <p>An Associative Array</p>
33: <pre>
34:<? echo $json02; ?>
35: </pre>
36:
37: <p>A Complex Array</p>
38: <pre>
39:<? echo $json03; ?>
40: </pre>
41:
42: <p>Complex Array with Name</p>
43: <pre>
44:<? echo $json04; ?>
45: </pre>
46:
47: <p>Simple JSon Format</p>
48: <pre>
49:<? echo myJsonData("Test Message", $assocArray01); ?>
50: </pre>
51:
52:</body>
53:</html>
Some of the later examples show how you can encode the JSON in a pretty specific format.