将JSON密钥转换为字符串

时间:2014-01-27 14:02:27

标签: php arrays json

我想转换关注json

{
    "one": {
        "two": {
            "three": {
                 "superNestedJSONBody": "please, help me",
                 "superNestedJSONBody2Again": "please, help me again"
            }
        }
    },
    "mySuperLongNameString": "is awesome"
}

为:

{
    "oneTwoThreeSuperNestedJSONBody": "please, help me",
    "mySuperLongNameString": "is awesome",
    "oneTwoThreeSuperNestedJSONBody2Again": "please, help me again"
}

我怎么能这样做?

我尝试过这个功能:

function convertJSONToStrings ($obj) {
    $result = Array();
    foreach ($obj as $key => $value) {
        !is_string($value) ? $result[$key] = convertJSONToStrings($value) : $result[$key] = $value;;
    }
    return $result;
}

但它不起作用

谢谢!

UDP

PHP 5.5.8(cli)(建于2014年1月12日19:34:38)

UDP [2]

更多澄清

2 个答案:

答案 0 :(得分:3)

您的问题似乎应该在JSON对象的类型上定义: 1.拥有多个第一级对象。 2.多个嵌套级别。 3.嵌套对象中没有相邻元素,仅在第一级中。 4.每个第一级应使用嵌套元素生成其键和值。

这是我的解决方案:

<?php
function convertJSONToStrings ($obj, $isfirst = true, $firstlevel = true) {
  if ($isfirst) $obj = json_decode($obj);
  elseif (is_string($obj)) return array('key' => '', val => $obj);

  if ($firstlevel) {
    $result = Array();
    foreach ($obj as $key => $val) {
      $s = convertJSONToStrings($val, false, false);
      $result[$key . $s['key']] = $s['val']; 
    }
    return $result;
  } else {
    foreach ($obj as $key => $val) {
      $s = convertJSONToStrings($val, false, false);
      return array('key' => $key . $s['key'], 'val' => $s['val']);
    }
  }
}

?>

快速测试:

<?php

$json = '{
    "one": {
        "two": {
            "three": {
                 "superNestedJSONBody": "please, help me"
            }
        }
    },
    "over": {
    },
    "mySuperLongNameString": "is awesome"
}';

print_r(convertJSONToStrings($json));

?>

希望它有所帮助!

更新#1

让我们支持多级JSON,这会将相邻的元素连接在一起,并且会在到达最深项目的路上考虑任何字符串。

进行测试并向我提供反馈意见!

function convertJSONToStrings ($obj, $isfirst = true, $firstlevel = true) {
  if ($isfirst) $obj = json_decode($obj);
  elseif (is_string($obj)) return array('key' => '', val => $obj);

  if ($firstlevel) {
    $result = Array();
    foreach ($obj as $key => $val) {
      $s = convertJSONToStrings($val, false, false);
      $result[$key . $s['key']] = $s['val']; 
    }
    return $result;
  } else {
    $wholeval = $wholekey = '';
    foreach ($obj as $key => $val) {
      $s = convertJSONToStrings($val, false, false);
      array('key' => $key . $s['key'], 'val' => $s['val']);
      $wholekey .= $key . $s['key'];
      $wholeval .= $s['val'];
    }
    return array('key' => $wholekey, 'val' => $wholeval);
  }
}

更新#2

在您对问题的最后一次澄清之后,以下是代码:

function convertJSONToStrings ($obj, $isfirst = true, $firstlevel = true) {
  if ($isfirst) $obj = json_decode($obj);
  elseif (is_string($obj)) return array(array('key' => '', val => $obj));

  if ($firstlevel) {
    $result = Array();
    foreach ($obj as $key => $val) {
      $s = convertJSONToStrings($val, false, false);
      foreach ($s as $o) {
        $result[$key . $o['key']] = $o['val'];
      } 
    }
    return $result;
  } else {
    $paths = array();
    foreach ($obj as $key => $val) {
      $s = convertJSONToStrings($val, false, false);
      foreach ($s as $o) {
        $paths[] = array('key' => $key . $o['key'], 'val' => $o['val']);
      }
    }
    return $paths;
  }
}

答案 1 :(得分:-1)

您需要的是此函数转换为数组:

function json2array($json) {
   if(get_magic_quotes_gpc()) {$json = stripslashes($json);}
   $json = str_replace(array(":", "{", "[", "}", "]"), array("=>", "array(", "array(", ")", ")"), $json);
   @eval("\$json_array = array({$json});");
   return $json_array;
}
$result = json2array($json);

如果您使用的是php4。

[编辑] 如果是php5:

$result = json_decode($json);