php - 强制 PHP 以原始格式传递数字
问题描述
我使用 PHP import_json 打开一个 JSON 文件,然后 export_json 仅将第一项输出为另一个 JSON 文件(本质上是将一个长 JSON 作业列表拆分为单个作业),但我遇到了一个问题,其中某处的行号是真实的或带有尾随“.0”的浮点数正在被转换并作为整数输出。
例子:
"Quality": -3.0,
"Quality": 20.0,
"AudioTrackGainSlider": 0.0,
"AudioTrackDRCSlider": 0.90000000000000002,
出来为:
"Quality":-3,
"Quality":20,
"AudioTrackDRCSlider":0.9,
"AudioTrackGainSlider":0,
我目前正在使用的整个 PHP 代码:
#open JSON file
$inputjson = json_decode(file_get_contents("store/queues/queue.json"), true);
#if empty echo "null"
if (is_null($inputjson[0])) {
echo "NULL";
} else {
#output first array index and print
header('Content-type: application/json');
$encodejson = array(0 => $inputjson[0]);
printf(json_encode($encodejson));
#Modify job list to remove first object and update index numbers
$outputjson = array();
$itemcount = count($inputjson);
$iterator = 1;
while ($iterator < $itemcount) {
$adjustment = $iterator - 1;
$pusharray = $inputjson[$iterator];
array_push($outputjson,$pusharray);
$iterator++;
}
#remove and re-create queue file with new generated queue
shell_exec('rm store/queues/queue.json');
shell_exec('touch store/queues/queue.json');
$jsoncreate = fopen("store/queues/queue.json", "w") or die("Unable to Write");
fwrite($jsoncreate, json_encode($outputjson));
fclose($jsoncreate);
}
示例 JSON 位于: https ://pastebin.com/K2qnggjT
不幸的是,我正在为其制作单个作业 JSON 的程序需要正确格式化数字,否则我会收到“预期的真实但得到 int”错误。JSON 文件包含字符串和数值,其中也有嵌套数组。
有什么方法可以让 PHP 像字符串一样传递数字,但在结果中仍显示为数值?在源代码中有显示为整数的数字,所以我认为仅将所有数字定义为带有尾随 .0 的浮点数是行不通的。这完全可以用 PHP 实现吗?
解决方案
推荐阅读
- javascript - 根据 BehaviorSubject 的值订阅 Observable
- winforms - 无法使用新数据更新文本框
- ios - SwiftUI 和 MVVM 设计模式
- oracle - 如何将子查询中的计数 ID 数分配给 plsql 声明的变量“v_count”
- php - 如何在给定模式上拆分字符串,同时将模式包含到拆分字符串的第二部分
- javascript - javaScript number.toString()
- angular - TS中复杂函数参数的解构
- r - R 中的数值错误(Wilcoxon 符号秩检验)
- python - 从出现最多的单词到出现较少的单词对列表进行排序
- angular - 如何嵌套 *ngFor 而不生成额外的 html?