drtoaamk20278 2018-08-01 20:13
浏览 86
已采纳

在php中调用json_decode后,如何连接和打印json对象中的值?

This is a schema generator that displays the key:value pair

 "default": [
                {
                    "one": "u0001u0000u0000u0000",
                    "two": "u0002u0000u0000u0000",
                    "three": "u0003u0000u0000u0000"
                }
            ]

What I would like to print out is "default": [{"u0001u0000u0000u0000u0002u0000u0000u0000u0003u0000u0000u0000"}]

Similarly if it is an object for instance:

"default": [ "a":
                {
                    "one": "u0001u0000u0000u0000",
                    "two": "u0002u0000u0000u0000",
                    "three": "u0003u0000u0000u0000"
                }
            ]

concatenate only values and print like this:

["a": {"u0001u0000u0000u0000u0002u0000u0000u0000u0003u0000u0000u0000"}]

Sample Code Test: // this method gets the value entered by user in json format. The user can put in a nested json format as well. The above examples that I mentioned works. it then calls for scanForNestedType method which scans whether the format contains any array, array>, map etc... Once it scans for nested type, it internally calls $this->encodeValues($unit)which converts the values entered by user from integer to bytes.

Here is an example:

User enters array [{"one": 1, "two": 2, "three": 3}]. After conversion, the result would be as follows: [ { "one": u0001u0000u0000u0000, "two": u00002u0000u0000u0000, "three: u0003u0000u0000u0000 } ]

Now I am getting the values correctly for each key. All I need is to print in this format: [ { u0001u0000u0000u0000u0002u0000u0000u0000u0003u0000u0000u0000 } ]

private function jsonDecode(array $value)
{
    $strValue = $value['value'];
    $jsonValue = json_decode($strValue);

    $this->scanForNestedType($jsonValue);
    return $jsonValue;
}

private function scanForNestedType(&$value)
{
    foreach ($value as $key => &$unit) {
        if (is_array($unit) || is_object($unit)) {
            $this->scanForNestedType($unit);
        } else {
            $value->$key = $this->encodeValues($unit);
        }
    }
}

private function encodeValues(int $value)
{
    $encodedValue = '';

    $bytesArray = unpack("C*", pack("V", $value));
    foreach ($bytesArray as $byte) {
        $encodedValue .= sprintf('u%04x', dechex($byte));
    }
    return $encodedValue;
}

If I get a working example then it would be great!

  • 写回答

1条回答 默认 最新

  • douxun7992 2018-08-01 22:20
    关注

    You can use json_decode to convert json to an array. Then implode sub-array ['one'....] using foreach loop.

    snippet

    $json1 = '[{"one": "u0001u0000u0000u0000", "two": "u0002u0000u0000u0000", "three": "u0003u0000u0000u0000"}]';
    $json2 = '[{ "a": { "one": "u0001u0000u0000u0000", "two": "u0002u0000u0000u0000", "three": "u0003u0000u0000u0000" } }]';
    $arr1 = json_decode($json1,true);
    $arr2 = json_decode($json2,true);
    
    
    foreach($arr1 as $data){
        $default1[] = implode($data);
    }
    $default1 = json_encode($default1, JSON_FORCE_OBJECT);
    
    foreach($arr2 as $key => $child){
        foreach($child as $childKey => $data){
            $default2[$childKey] = implode($data);
        }
    }
    $default2 = json_encode($default2, JSON_FORCE_OBJECT);
    
    print_r($default1);
    print_r($default2);
    

    Output

    {"0":"u0001u0000u0000u0000u0002u0000u0000u0000u0003u0000u0000u0000"}
    {"a":"u0001u0000u0000u0000u0002u0000u0000u0000u0003u0000u0000u0000"}
    

    Live Demo

    You can now json_decode $default1, $default2 to get php object/array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择