1//Json Encode
2
3$person = array(
4 "name" => "KINGASV",
5 "title" => "CTO"
6);
7$personJSON=json_encode($person);//returns JSON string
8
9//Json Decode
10
11$personJSON = '{"name":"KINGASV","title":"CTO"}';
12
13$person = json_decode($personJSON);
14
15echo $person->name; // KINGASV
16
1$personJSON = '{"name":"Johny Carson","title":"CTO"}';
2
3$person = json_decode($personJSON);
4
5echo $person->name; // Johny Carson
1var jsonPerson = '{"first_name":"billy", "age":23}';
2var personObject = JSON.parse(jsonPerson); //parse json string into JS object
1const json = '{ "fruit": "pineapple", "fingers": 10 }';
2const obj = JSON.parse(json);
3console.log(obj.fruit, obj.fingers);
1json_encode used when PHP retrieve data and convert Array() to [] !!!!
2$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
3echo json_encode($arr);
4//output
5{"a":1,"b":2,"c":3,"d":4,"e":5}
6access from js file data.a, data.b,data.c...
7----------------------------------------------
8 $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
9json_decode($json, true); //true turns object to associative array;
10
11//output
12array(5) {
13 ["a"] => int(1)
14 ["b"] => int(2)
15 ["c"] => int(3)
16 ["d"] => int(4)
17 ["e"] => int(5)
18}
19
20