3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JSON を PHP の Array にする JavaScript

Last updated at Posted at 2017-02-24

需要があるかわからないけど、作ったので。
作ったよ!っていうのが大事かなって。

jsonToPhp.js
/**
 * textJson に渡された文字列の JSON を PHP の Array に変換する
 * 
 * @param {string} textJson
 * @returns {string} PHP style array
 */
function jsonToPhpArray(textJson) {
  var values = textJson.replace(/\r/g, '').split('\n').map(function (v) {
    return v.trim();
  });

  var ary = [];
  for (var i = 0; i < values.length; i++) {
    var v = values[i];
    if (v.indexOf('//') == 0) {
      // コメントは飛ばす
      continue;
    }
    ary.push(v);
  }
  var json = JSON.parse(ary.join(''));
  return '$values = [' + _jsonToPhpArray(json) + '];';
}

/**
 * Array 判定
 * 
 * @param v
 * @returns {boolean}
 */
function isArray(v) {
  return Object.prototype.toString.call(v) === '[object Array]';
}

/**
 * Object 判定
 * 
 * @param v
 * @returns {boolean}
 */
function isObject(v) {
  return Object.prototype.toString.call(v) === '[object Object]';
}

/**
 * @param {Object} json
 * @returns {string}
 * @private
 */
function _jsonToPhpArray(json) {
  var ret = '';
  var keys = Object.keys(json);
  for (var i = 0; i < keys.length; i++) {
    var key = keys[i];
    var value = json[key];

    if (isArray(value)) {
      // 配列の場合
      ret += '"' + key + '" => ' + _arrayToPhpArray(value) + ', ';
    } else if (isObject(value)) {
      // オブジェクトの場合
      ret += '"' + key + '" => [' + _jsonToPhpArray(value) + '], ';
    } else if (value == null) {
      // null/undefined の場合
      ret += _toPhpArrayStringWithKey(key, 'null', value);
    } else if (typeof value === 'string') {
      // 文字列の場合
      ret += _toPhpArrayStringWithKey(key, 'string', value);
    } else if (typeof value === 'number') {
      // 数値の場合
      ret += _toPhpArrayStringWithKey(key, 'number', value);
    } else if (typeof value === 'boolean') {
      // 真偽値の場合
      ret += _toPhpArrayStringWithKey(key, 'boolean', value);
    } else {
      // それ以外は何もしない (Functionくらいか)
    }
  }

  return ret;
}

/**
 * @param {Array} array
 * @returns {string}
 * @private
 */
function _arrayToPhpArray(array) {
  var ret = '[';
  for (var i = 0; i < array.length; i++) {
    var value = array[i];

    if (isArray(value)) {
      // 配列の場合
      ret += _arrayToPhpArray(value);
    } else if (isObject(value)) {
      // オブジェクトの場合
      ret += _jsonToPhpArray(value);
    } else if (value == null) {
      // null/undefined の場合
      ret += _toPhpArrayString('null', value);
    } else if (typeof value === 'string') {
      // 文字列の場合
      ret += _toPhpArrayString('string', value);
    } else if (typeof value === 'number') {
      // 数値の場合
      ret += _toPhpArrayString('number', value);
    } else if (typeof value === 'boolean') {
      // 真偽値の場合
      ret += _toPhpArrayString('boolean', value);
    } else {
      // それ以外は何もしない (Functionくらいか)
    }
  }

  return ret + ']';
}

/**
 * @param type
 * @param value
 * @returns {string}
 * @private
 */
function _toPhpArrayString(type, value) {
  var retStr;
  if (type === 'null') {
    retStr = 'null,';
  } else if (type === 'string') {
    retStr = '"' + value + '",';
  } else {
    retStr = value + ',';
  }
  return retStr;
}

/**
 * @param key
 * @param type
 * @param value
 * @returns {string}
 * @private
 */
function _toPhpArrayStringWithKey(key, type, value) {
  var retStr;
  if (type === 'null') {
    retStr = '"' + key + '" => null, ';
  } else if (type === 'string') {
    retStr = '"' + key + '" => "' + value + '", ';
  } else {
    retStr = '"' + key + '" => ' + value + ', ';
  }
  return retStr;
}
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?