GithubHelp home page GithubHelp logo

json-parse-by-php's Introduction

JSON parsing by PHP

Just to learn JSON parsing.

<?php

require_once __DIR__ . '/Parser.php';


try {
    $parser = new Parser(<<<JSON
{
  "a": 1e2,
  "b": "c",
  "c": {
    "d": 3,
    "c": [1, 3, 4, {
      "d": 3
    }]
  },
  "e": 2.3,
  "de": {
    "true": true,
    "false": false,
    "null": null
  }
}
JSON
    );
    var_dump($parser->decode());

} catch (\Exception $ex) {
    var_dump($ex->getMessage());
}

// php8 parser.php | sed 's/^/\/\/ /g'
// array(5) {
//   ["a"]=>
//   int(100)
//   ["b"]=>
//   string(1) "c"
//   ["c"]=>
//   array(2) {
//     ["d"]=>
//     int(3)
//     ["c"]=>
//     array(4) {
//       [0]=>
//       int(1)
//       [1]=>
//       int(3)
//       [2]=>
//       int(4)
//       [3]=>
//       array(1) {
//         ["d"]=>
//         int(3)
//       }
//     }
//   }
//   ["e"]=>
//   float(2.3)
//   ["de"]=>
//   array(3) {
//     ["true"]=>
//     bool(true)
//     ["false"]=>
//     bool(false)
//     ["null"]=>
//     NULL
//   }
// }

JSON decode

<?php

require_once __DIR__ . '/ToJson.php';

try {
    $toJSON = new ToJson();
    $res = $toJSON->stringify([1, 2, 4.3, 3, ['a' => 'b', 'c' => 1, 'd' => [1, 2, 3]]]);
    echo $res, PHP_EOL;
    echo PHP_EOL;
    var_dump(json_decode($res, true));
    echo PHP_EOL;
    $res = $toJSON->stringify([
        'a' => 2.3,
        'b' => false,
        'd' => true,
        'null' => null,
        'c' => -1,
        'eee' => [1, 2, 3, [
            'c' => [
                'a' => 'c',
                'c' => [2, 3]
            ]
        ]],
        'string' => 'sfsadf"sdfsadfsf'
    ], true);
    echo $res, PHP_EOL;
    echo PHP_EOL;
    var_dump(json_decode($res, true));
} catch (\Exception $ex) {
    var_dump($ex->getMessage());
}

// php8 test.php | sed 's/^/\/\/  /g'
//  [1, 2, 4.3, 3, {"a": "b", "c": 1, "d": [1, 2, 3]}]
//  
//  array(5) {
//    [0]=>
//    int(1)
//    [1]=>
//    int(2)
//    [2]=>
//    float(4.3)
//    [3]=>
//    int(3)
//    [4]=>
//    array(3) {
//      ["a"]=>
//      string(1) "b"
//      ["c"]=>
//      int(1)
//      ["d"]=>
//      array(3) {
//        [0]=>
//        int(1)
//        [1]=>
//        int(2)
//        [2]=>
//        int(3)
//      }
//    }
//  }
//  
//  {
//    "a": 2.3, 
//    "b": false, 
//    "d": true, 
//    "null": null, 
//    "c": -1, 
//    "eee": [
//      1, 
//      2, 
//      3, 
//      {
//        "c": {
//          "a": "c", 
//          "c": [
//            2, 
//            3
//          ]
//        }
//      }
//    ], 
//    "string": "sfsadf\"sdfsadfsf"
//  }
//  
//  array(7) {
//    ["a"]=>
//    float(2.3)
//    ["b"]=>
//    bool(false)
//    ["d"]=>
//    bool(true)
//    ["null"]=>
//    NULL
//    ["c"]=>
//    int(-1)
//    ["eee"]=>
//    array(4) {
//      [0]=>
//      int(1)
//      [1]=>
//      int(2)
//      [2]=>
//      int(3)
//      [3]=>
//      array(1) {
//        ["c"]=>
//        array(2) {
//          ["a"]=>
//          string(1) "c"
//          ["c"]=>
//          array(2) {
//            [0]=>
//            int(2)
//            [1]=>
//            int(3)
//          }
//        }
//      }
//    }
//    ["string"]=>
//    string(16) "sfsadf"sdfsadfsf"
//  }

json-parse-by-php's People

Contributors

deliangyang avatar

Watchers

 avatar  avatar  avatar

json-parse-by-php's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.