项目作者: oknoorap

项目描述 :
:sunny: Convert JSON to PHP native Array.
高级语言: JavaScript
项目地址: git://github.com/oknoorap/jsonar.git
创建时间: 2017-05-17T07:35:04Z
项目社区:https://github.com/oknoorap/jsonar

开源协议:MIT License

下载


:sunny: JSONAR

Convert JSON or javascript object to PHP native Array.

Install

Using NPM
npm install jsonar --save

Using Yarn
yarn add jsonar

Usage

  1. const fs = require('fs')
  2. const path = require('path')
  3. const jsonar = require('jsonar')
  4. const jsonStr = fs.readFileSync(path.join(__dirname, 'test.json'), 'ascii')
  5. const world = 'World'
  6. console.log(jsonStr)
  7. /**
  8. * Example of json file
  9. {
  10. "greetings": "Hello",
  11. "answers": 42,
  12. "slug": jsonar.literal(`slugify_fn("Hello ${world}")`),
  13. "inception": {
  14. "nested": {
  15. "object": true
  16. },
  17. "array": [
  18. "string",
  19. true,
  20. 100,
  21. {
  22. "inception": true
  23. }
  24. ]
  25. },
  26. "playlist": [
  27. {
  28. "id": "DHyUYg8X31c",
  29. "desc": "Do Robots Deserve Rights? What if Machines Become Conscious?"
  30. },
  31. {
  32. "id": "ijFm6DxNVyI",
  33. "desc": "The Most Efficient Way to Destroy the Universe - False Vacuum"
  34. }
  35. ]
  36. }
  37. */
  38. const phpArray = jsonar.arrify(jsonStr, {prettify: true})

PHP Array Output

  1. array(
  2. "greetings" => "Hello",
  3. "answers" => 42,
  4. "slug" => slug_fn("Hello World"),
  5. "inception" => array(
  6. "nested" => array(
  7. "object" => true
  8. ),
  9. "array" => array(
  10. "string",
  11. true,
  12. 100,
  13. array(
  14. "inception" => true
  15. )
  16. )
  17. ),
  18. "playlist" => array(
  19. array(
  20. "id" => "DHyUYg8X31c",
  21. "desc" => "Do Robots Deserve Rights? What if Machines Become Conscious?"
  22. ),
  23. array(
  24. "id" => "ijFm6DxNVyI",
  25. "desc" => "The Most Efficient Way to Destroy the Universe - False Vacuum"
  26. )
  27. )
  28. );
  1. // Convert array to json again
  2. jsonar.parse(`your php array string here`)

Methods

jsonar.arrify(json: Object | String, options: Object)

Arguments Description
json JSON string or Javascript Object.
options Default options are: { prettify: false, indent: 1, space: false, quote: jsonar.quoteTypes.DOUBLE }

jsonar.parse(phpString: String, options: Boolean)

Arguments Description
phpString PHP String.
options Default options are: { asObject: true, emptyRules: {} }, emptyRules should be objects that has the same structure with your object, if you don’t set emptyRules all empty objects will be converted as primitive array [], because PHP AST read an empty object as array

jsonar.literal(string)

Constants

jsonar.quoteTypes.SINGLE = '
jsonar.quoteTypes.DOUBLE = "

License

MIT © oknoorap