这是我的问题。我有一个像这样的JSON文件:
[
{
"projectName": "test",
"clientName": "test2",
"dateValid": "2014-04-18",
"account": {
"accountAmount": null,
"accountDate": "2014-04-19",
"accountType": null
},
"total": {
"totalAmount": null,
"totalDate": "2014-04-18",
"totalType": null
}
}]
我希望PHP打开这个文件并添加另一个对象,所以我的文件将如下所示:
[
{
"projectName": "test",
"clientName": "test2",
"dateValid": "2014-04-18",
"account": {
"accountAmount": null,
"accountDate": "2014-04-19",
"accountType": null
},
"total": {
"totalAmount": null,
"totalDate": "2014-04-18",
"totalType": null
}
},
{
"projectName": "test",
"clientName": "test2",
"dateValid": "2014-04-18",
"account": {
"accountAmount": null,
"accountDate": "2014-04-19",
"accountType": null
},
"total": {
"totalAmount": null,
"totalDate": "2014-04-18",
"totalType": null
}
}
]
应该很简单,但我无法做到。我尝试了多种方法:
$file = 'base.json';
if(file_exists ($file)){
echo 'base.json found';
$fileContent = file_get_contents($file);
$oldData = json_decode($fileContent, true);
echo var_export($oldData);
}
else {
echo 'base.json not found';
$oldData = [];
}
echo $data;
$data = json_encode($data);
$oldData = json_encode($oldData);
echo $data; // debug
file_put_contents('base.json', '['.$data.','.$oldData.']');
是的,我投入了大量的回音调试数据流程......我缺少什么?
答案 0 :(得分:5)
你将此视为字符串操作,这是一种错误的方法。您需要在两个结构对象时将它们组合在一起,之前将它们重新编码为JSON。
这三行......
$data = json_encode($data);
$oldData = json_encode($oldData);
file_put_contents('base.json', '['.$data.','.$oldData.']');
应改写为......
// Create a new array with the new data, and the first element from the old data
$newData = array($data, $oldData[0]);
$newData = json_encode($newData);
file_put_contents('base.json', $newData);
答案 1 :(得分:2)
使用以下命令将新数据添加到数组中:
$oldData[] = $data;
然后将其写回文件:
file_put_contents('base.json', json_encode($oldData));
答案 2 :(得分:2)
您可以将json var转换为数组,在json_decode上将第二个param指定为true,并在使用array_merge包含新数组var并再次转换为json之后。
<?php
$json1 = '[{
"projectName": "test",
"clientName": "test2",
"dateValid": "2014-04-18",
"account": {
"accountAmount": null,
"accountDate": "2014-04-19",
"accountType": null
},
"total": {
"totalAmount": null,
"totalDate": "2014-04-18",
"totalType": null
}
}]';
$json2 = '[{
"projectName": "test 2",
"clientName": "test3",
"dateValid": "2014-04-22",
"account": {
"accountAmount": null,
"accountDate": "2014-04-27",
"accountType": null
},
"total": {
"totalAmount": null,
"totalDate": "2014-04-27",
"totalType": null
}
}]';
$arr1 = json_decode($json1, true);
$arr2 = json_decode($json2, true);
$json2 = json_encode(array_merge($arr1, $arr2));
?>
答案 3 :(得分:1)
试试这个
$arr = json_decode(file_get_contents('myFile.json'));
// append a new "object" (array)
$arr[] = array(
"projectName" => "test",
"clientName" => "test2",
"dateValid" => "2014-04-18",
"account" => array(
"accountAmount" => null,
"accountDate" => "2014-04-19",
"accountType" => null
),
"total" => array(
"totalAmount" => null,
"totalDate" => "2014-04-18",
"totalType" => null
)
);
$json = json_encode($arr);
file_put_contents('myFile.json', $json);
答案 4 :(得分:0)
试试这个:
<?php
$json = '[
{
"projectName": "test",
"clientName": "test2",
"dateValid": "2014-04-18",
"account": {
"accountAmount": null,
"accountDate": "2014-04-19",
"accountType": null
},
"total": {
"totalAmount": null,
"totalDate": "2014-04-18",
"totalType": null
}
}]';
$json_to_add=' {
"projectName": "test",
"clientName": "test2",
"dateValid": "2014-04-18",
"account": {
"accountAmount": null,
"accountDate": "2014-04-19",
"accountType": null
},
"total": {
"totalAmount": null,
"totalDate": "2014-04-18",
"totalType": null
}
}';
$data = json_decode($json);
$data_to_add = json_decode($json_to_add);
$data[]=$data_to_add ;
var_dump($data);