首页 > 解决方案 > 如何修复 JSON 响应中 Datetime 带来的空间

问题描述

我想删除从日期时间获得的空间。因为这个 JSON 格式无效。以下是我的 JSON 响应:

{
"txId": "226334",
"createdAt":"2019-06-20
18: 33: 51","details":"Transfered
out","account_name_2":"","currency":"myr","amount":"-44.00","balance":"44,
272.80"}

下面是我的代码

$data['transactions'] = [];
        foreach ($transactions as $tx) {
            $data['transactions'][] = array(
                'txId' => $tx['id'],
                'createdAt' => trim($tx['created_at']),
                'details' => str_replace(" "," ",$this->lang->line($tx['description'])),
                'account_name_2' => isset($merchants[$tx['account_name_2']]) ? $merchants[$tx['account_name_2']] : $tx['account_name_2'],
                'currency' => $currency,
                'amount' => $tx['amount'] > 0 ? '+' . number_format($tx['amount'] * $forex['buy_in'], 2) : number_format($tx['amount'] * $forex['buy_in'], 2),
                'balance' => number_format($total * $forex['buy_in'], 2)
            );
            $total -= number_format($tx['amount'] * $forex['buy_in'], 2);
        }

        $this->respSuccess($data);

标签: phpjson

解决方案


尝试str_replace

str_replace("\n","",$json);

如果需要,可以用空格替换str_replace("\n"," ",$json);

在这里检查


推荐阅读