【问题标题】:PHP json_decode questionPHP json_decode 问题
【发布时间】:2009-11-11 08:11:13
【问题描述】:

我正在尝试使用 json_decode 来组合一些 json 对象,然后对其进行重新编码。我的 json 看起来像:

{
    "core": {
        "segment": [
            {
                "id": 7,
                "name": "test1" 
            },
            {
                "id": 4,
                "name": "test2" 
            } 
        ] 
    }
}

我有一些这样的 json 对象,并且想只为每个组合“段”数组以获得如下结果:

{
    "segment": [
        {
            "id": 7,
            "name": "test1" 
        },
        {
            "id": 4,
            "name": "test2" 
        } 
    ],
    "segment": [
        {
            "id": 5,
            "name": "test3" 
        },
        {
            "id": 8,
            "name": "test4" 
        } 
    ]
}

现在在我的 php 代码中,我正在解码 json,将每个“段”数组存储到一个字符串中,然后对 json 进行编码。

public function handleJSON($json){
        $decodeData = json_decode($json);
        $segment =$decodeData->core;
        return $segment;
}

public function formatJSON(){
        $segments = "";
    for ($i = 0; $i < count($json);$i++)
        {
          $segments .= handleJSON($json[$i]);
        }
    echo json_encode($segments);
}

当我这样做时,我收到一个错误: 类 stdClass 的对象无法转换为字符串

然后我尝试使用将它们存储在数组中:

public function formatJSON(){
        $segments = array();
    for ($i = 0; $i < count($json);$i++)
        {
          $segments[$i] = handleJSON($json[$i]);
        }
    echo json_encode($segments);
}

这一次,我没有收到错误,但它将我的整个组合 json 对象存储在数组括号中。我怎样才能让它只返回 JSON 对象,而不被封装在一个数组中?

【问题讨论】:

    标签: php json


    【解决方案1】:

    我认为一种方法是利用json_decode、assoc 的第二个参数:

    "当 TRUE 时,返回的对象将是 转换成关联数组。”

    我发现通常处理关联数组比处理stdClass 类更容易。

    $str = '{
       "core": {
            "segment": [
                {
                    "id": 7,
                    "name": "test1" 
                },
                {
                    "id": 4,
                    "name": "test2" 
                } 
            ] 
        }
    }';
    print "<pre>";
    print_r(json_decode($str));
    print "</pre>";
    print "<pre>";
    print_r(json_decode($str,true));
    print "</pre>";
    

    这首先产生对象版本,然后是关联数组:

    stdClass Object
    (
        [core] => stdClass Object
            (
                [segment] => Array
                    (
                        [0] => stdClass Object
                            (
                                [id] => 7
                                [name] => test1
                            )
    
                        [1] => stdClass Object
                            (
                                [id] => 4
                                [name] => test2
                            )
    
                    )
    
            )
    
    )
    Array
    (
        [core] => Array
            (
                [segment] => Array
                    (
                        [0] => Array
                            (
                                [id] => 7
                                [name] => test1
                            )
    
                        [1] => Array
                            (
                                [id] => 4
                                [name] => test2
                            )
    
                    )
    
            )
    
    )
    

    我想我会做类似的事情,创建新的空白数组,解码为关联数组,取出段成员并将它们拼接到新的空白数组中。所以:

    $segments = array();
    // assuming you had a bunch of items in the $strings array
    foreach ($strings as $str) {
      $item = json_decode($str,true);
      $segments = array_merge($item['core']['segment], $segments);
    }
    

    现在,您可以像这样将其编码为 json:

    $final_json = json_encode(array('segments'=>$segments));
    

    【讨论】:

    • 谢谢!这太有帮助了,太荒谬了!
    【解决方案2】:

    您的外部对象包含两个名为“段”的项目。虽然这是合法的 JSON,但 PHP 对象不可能有两个具有相同名称的不同项目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-17
      • 2011-03-07
      • 1970-01-01
      相关资源
      最近更新 更多