我正在尝试将json文件转换为csv,但是仅文本“ array,array”被打印在csv文件中。我猜这是因为返回了多个数组,但是作为菜鸟,我不知道如何解决这个问题。
<?php
$jsonString = file_get_contents("data.json");
//Decode the JSON and convert it into an associative array.
$jsonDecoded = json_decode($jsonString, true);
$jsonDecoded = $jsonDecoded;
//Give our CSV file a name.
$csvFileName = 'example.csv';
//Open file pointer.
$fp = fopen($csvFileName, 'w');
//Loop through the associative array.
foreach($jsonDecoded as $row){
//Write the row to the CSV file.
fputcsv($fp, $row);
}
//Finally, close the file pointer.
fclose($fp);
print $jsonDecoded;
echo json_last_error_msg();
?>
json看起来像这样,包含多个记录,并且应该每行打印一次。
{"data":[{"ID":4,"UUID":"53F","A schematic overview of your activities":"Yes","Q1_1-1":"To some extent","Q1_1-2":"To some extent","Q1_1-3":"To some extent","Question 1_2":"Yes","Q1_2-1":"Yes","Q1_2-2":"To some extent","Q1_2-3":"No","Q1_2-4":"Yes","Q1_2-5":"To some extent","Q1_2-6":"Yes","Question 1_3":"Yes","Q1_3-1":"Yes","Q1_3-2":"To some extent","Q1_3-3":"To some extent","Q1_3-4":"No","Q1_3_5":"Yes","Question 2":"To some extent","Q2_2":"To some extent","Q2_3":"To some extent","Q2_4":"To some extent","Question 3":"No","Q3_2":"No","Q3_3":"To some extent","Q3_4":"Yes","Question 3_2":"Yes","Q3_2-2":"Yes","Q3_2-3":"To some extent","Question 3_3":"No","Q3_3-2":"To some extent","Q3_3-3":"Yes","Q3_3-4":"To some extent","Question 3_4":"Yes","Q3_4-2":"To some extent","Q3_4-3":"To some extent","Q3_4-4":"To some extent","Question 3_5":"Yes","Q3_5-2":"To some extent","Q3_5-3":"Yes","Q3_5-4":"Yes","Q3_5_5":"To some extent","Q3_5-6":"To some extent","Question 3_6":"Yes","Q3_6-2":"Yes","CreatedAt":"2019-08-14T10:38:07.033Z","CreatedBy":"qqq","UpdatedAt":null,"UpdatedBy":null,"CreatedByID":20,"UpdatedByID":null},{"ID":5,"UUID":"2D40","A schematic overview of your activities":"Yes","Q1_1-1":"To some extent","Q1_1-2":"To some extent","Q1_1-3":"Yes","Question 1_2":"Yes","Q1_2-1":"To some extent","Q1_2-2":"No","Q1_2-3":"To some extent","Q1_2-4":"Yes","Q1_2-5":"Yes","Q1_2-6":"To some extent","Question 1_3":null,"Q1_3-1":null,"Q1_3-2":null,"Q1_3-3":null,"Q1_3-4":null,"Q1_3_5":null,"Question 2":null,"Q2_2":null,"Q2_3":null,"Q2_4":null,"Question 3":"No","Q3_2":"To some extent","Q3_3":"To some extent","Q3_4":"To some extent","Question 3_2":"Yes","Q3_2-2":"To some extent","Q3_2-3":"Yes","Question 3_3":"Yes","Q3_3-2":"No","Q3_3-3":"To some extent","Q3_3-4":"Yes","Question 3_4":"Yes","Q3_4-2":"To some extent","Q3_4-3":"Yes","Q3_4-4":"Yes","Question 3_5":"No","Q3_5-2":"To some extent","Q3_5-3":"To some extent","Q3_5-4":"Yes","Q3_5_5":"To some extent","Q3_5-6":"To some extent","Question 3_6":"Yes","Q3_6-2":"To some extent","CreatedAt":"2019-08-19T13:48:22.770Z","CreatedBy":"qqq","UpdatedAt":null,"UpdatedBy":null,"CreatedByID":20,"UpdatedByID":null}]}
答案 0 :(得分:0)
您只需要参考'data'
键即可。
foreach($jsonDecoded['data'] as $row){ ...
答案 1 :(得分:0)
https://www.php.net/manual/en/splfileobject.fputcsv.php
$jsonString = file_get_contents("data.json");
//Decode the JSON and convert it into an associative array.
$jsonDecoded = json_decode($jsonString, true);
$list= $jsonDecoded;
file = new SplFileObject('example.csv', 'w');
foreach ($list as $fields) {
$file->fputcsv($fields);
}