项目作者: deliangyang

项目描述 :
JSON decode/encode by PHP
高级语言: PHP
项目地址: git://github.com/deliangyang/json-parse-by-php.git
创建时间: 2021-04-10T03:15:57Z
项目社区:https://github.com/deliangyang/json-parse-by-php

开源协议:

下载


JSON parsing by PHP

Just to learn JSON parsing.

  1. <?php
  2. require_once __DIR__ . '/Parser.php';
  3. try {
  4. $parser = new Parser(<<<JSON
  5. {
  6. "a": 1e2,
  7. "b": "c",
  8. "c": {
  9. "d": 3,
  10. "c": [1, 3, 4, {
  11. "d": 3
  12. }]
  13. },
  14. "e": 2.3,
  15. "de": {
  16. "true": true,
  17. "false": false,
  18. "null": null
  19. }
  20. }
  21. JSON
  22. );
  23. var_dump($parser->decode());
  24. } catch (\Exception $ex) {
  25. var_dump($ex->getMessage());
  26. }
  27. // php8 parser.php | sed 's/^/\/\/ /g'
  28. // array(5) {
  29. // ["a"]=>
  30. // int(100)
  31. // ["b"]=>
  32. // string(1) "c"
  33. // ["c"]=>
  34. // array(2) {
  35. // ["d"]=>
  36. // int(3)
  37. // ["c"]=>
  38. // array(4) {
  39. // [0]=>
  40. // int(1)
  41. // [1]=>
  42. // int(3)
  43. // [2]=>
  44. // int(4)
  45. // [3]=>
  46. // array(1) {
  47. // ["d"]=>
  48. // int(3)
  49. // }
  50. // }
  51. // }
  52. // ["e"]=>
  53. // float(2.3)
  54. // ["de"]=>
  55. // array(3) {
  56. // ["true"]=>
  57. // bool(true)
  58. // ["false"]=>
  59. // bool(false)
  60. // ["null"]=>
  61. // NULL
  62. // }
  63. // }

JSON decode

  1. <?php
  2. require_once __DIR__ . '/ToJson.php';
  3. try {
  4. $toJSON = new ToJson();
  5. $res = $toJSON->stringify([1, 2, 4.3, 3, ['a' => 'b', 'c' => 1, 'd' => [1, 2, 3]]]);
  6. echo $res, PHP_EOL;
  7. echo PHP_EOL;
  8. var_dump(json_decode($res, true));
  9. echo PHP_EOL;
  10. $res = $toJSON->stringify([
  11. 'a' => 2.3,
  12. 'b' => false,
  13. 'd' => true,
  14. 'null' => null,
  15. 'c' => -1,
  16. 'eee' => [1, 2, 3, [
  17. 'c' => [
  18. 'a' => 'c',
  19. 'c' => [2, 3]
  20. ]
  21. ]],
  22. 'string' => 'sfsadf"sdfsadfsf'
  23. ], true);
  24. echo $res, PHP_EOL;
  25. echo PHP_EOL;
  26. var_dump(json_decode($res, true));
  27. } catch (\Exception $ex) {
  28. var_dump($ex->getMessage());
  29. }
  30. // php8 test.php | sed 's/^/\/\/ /g'
  31. // [1, 2, 4.3, 3, {"a": "b", "c": 1, "d": [1, 2, 3]}]
  32. //
  33. // array(5) {
  34. // [0]=>
  35. // int(1)
  36. // [1]=>
  37. // int(2)
  38. // [2]=>
  39. // float(4.3)
  40. // [3]=>
  41. // int(3)
  42. // [4]=>
  43. // array(3) {
  44. // ["a"]=>
  45. // string(1) "b"
  46. // ["c"]=>
  47. // int(1)
  48. // ["d"]=>
  49. // array(3) {
  50. // [0]=>
  51. // int(1)
  52. // [1]=>
  53. // int(2)
  54. // [2]=>
  55. // int(3)
  56. // }
  57. // }
  58. // }
  59. //
  60. // {
  61. // "a": 2.3,
  62. // "b": false,
  63. // "d": true,
  64. // "null": null,
  65. // "c": -1,
  66. // "eee": [
  67. // 1,
  68. // 2,
  69. // 3,
  70. // {
  71. // "c": {
  72. // "a": "c",
  73. // "c": [
  74. // 2,
  75. // 3
  76. // ]
  77. // }
  78. // }
  79. // ],
  80. // "string": "sfsadf\"sdfsadfsf"
  81. // }
  82. //
  83. // array(7) {
  84. // ["a"]=>
  85. // float(2.3)
  86. // ["b"]=>
  87. // bool(false)
  88. // ["d"]=>
  89. // bool(true)
  90. // ["null"]=>
  91. // NULL
  92. // ["c"]=>
  93. // int(-1)
  94. // ["eee"]=>
  95. // array(4) {
  96. // [0]=>
  97. // int(1)
  98. // [1]=>
  99. // int(2)
  100. // [2]=>
  101. // int(3)
  102. // [3]=>
  103. // array(1) {
  104. // ["c"]=>
  105. // array(2) {
  106. // ["a"]=>
  107. // string(1) "c"
  108. // ["c"]=>
  109. // array(2) {
  110. // [0]=>
  111. // int(2)
  112. // [1]=>
  113. // int(3)
  114. // }
  115. // }
  116. // }
  117. // }
  118. // ["string"]=>
  119. // string(16) "sfsadf"sdfsadfsf"
  120. // }