我试图制作JSON响应,但我在响应数据中遇到问题
{"data":{"balance":"1000.21","currency":"CNY"},"status":{"code":"0","message":"Success","datetime":"2018-04-09T12:06:20+08:00"}}
我需要将show show显示为float,数字类型,但它显示为字符串 所以这是我需要的正确数据
{"data":{"balance": 1000.21,"currency":"CNY"},"status":{"code":"0","message":"Success","datetime":"2018-04-09T12:06:20+08:00"}}
我的代码
response->setStatusCode(200, 'OK')->sendHeaders();
$response->setContentType('application/json', 'utf-8');
$resData = array(
'data' => [
'balance' => $user->balance,
'currency' => $user->currencyCode
],
'status' => [
'code' => '0',
'message' => 'Success',
'datetime' => $this->getDateTime()
]
);
$response->setJsonContent($resData);
return $response;
答案 0 :(得分:0)
<?php
$data = '{"data":{"balance":"1000.21","currency":"CNY"},"status":{"code":"0","message":"Success","datetime":"2018-04-09T12:06:20+08:00"}}';
//you can loop through the whole data and then convert again to json
$data = json_decode($data,true);
$response = array();
$response['data'] = array();
$response['status'] = $data['status'];
foreach($data as $array){
$array['balance'] = (float)$array;
$response['data'][] = $array;
}
print_r(json_encode($data,true));
?>