douzhuanfen5923 2017-03-14 11:49
浏览 295
已采纳

使用php将json转换为csv

Hello i have small problem with converting json to csv. Here is my code:

$jsonString = '{"cod":"200","calctime":0.3107,"cnt":15,"list":[{"id":2208791,"name":"Yafran","coord":{"lon":12.52859,"lat":32.06329},"main":{"temp":9.68,"temp_min":9.681,"temp_max":9.681,"pressure":961.02,"sea_level":1036.82,"grnd_level":961.02,"humidity":85},"dt":1485784982,"wind":{"speed":3.96,"deg":356.5},"rain":{"3h":0.255},"clouds":{"all":88},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}]}]}';

//Decode the JSON and convert it into an associative array.
$jsonDecoded = json_decode($jsonString, true);

//Give our CSV file a name.
$csvFileName = 'file.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);

?>

I have tried with another json format like this [{"name":"Wayne","age":28},{"name":"John","age":21},{"name":"Sara","age":24}] and its working perfect. How to modify my code to save it correctly in csv format.

Pictures: Now it save it like this: pic 1 I need to save it like this: pic 2

Can someone help me ?

  • 写回答

2条回答 默认 最新

  • duanlongnao0028 2017-03-14 12:10
    关注

    Hope this will work..

    <?php
    
    $jsonString = '{"cod":"200","calctime":0.3107,"cnt":15,"list":[{"id":2208791,"name":"Yafran","coord":{"lon":12.52859,"lat":32.06329},"main":{"temp":9.68,"temp_min":9.681,"temp_max":9.681,"pressure":961.02,"sea_level":1036.82,"grnd_level":961.02,"humidity":85},"dt":1485784982,"wind":{"speed":3.96,"deg":356.5},"rain":{"3h":0.255},"clouds":{"all":88},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}]}]}';
    
    $jsonDecoded = json_decode($jsonString, true);
    $csvHeader=array();
    $csvData=array();
    jsontocsv($jsonDecoded);
    print_r($csvHeader);
    print_r($csvData);
    
    
    
    $csvFileName = 'file.csv';
    $fp = fopen($csvFileName, 'w');
    fputcsv($fp, $csvHeader);
    fputcsv($fp, $csvData);
    fclose($fp);
    
    function jsontocsv($data)
    {
        global $csvData,$csvHeader;
        foreach($data as $key => $value)
        {
            if(!is_array($value))
            {
                $csvData[]=$value;
                $csvHeader[]=$key;
            }
            else 
            {
                jsontocsv($value);
            }
        }
    }
    

    Json 2

    <?php
    
    $jsonString =file_get_contents("http://samples.openweathermap.org/data/2.5/box/city?bbox=12,32,15,37,10&appid=b1b15e88fa797225412429c1c50c122a1");;
    $jsonDecoded = json_decode($jsonString, true);
    $csvHeader=array();
    $csvData=array();
    $csvFileName = 'file.csv';
    $fp = fopen($csvFileName, 'w');
    $counter=0;
    foreach($jsonDecoded["list"] as $key => $value)
    {
        jsontocsv($value);
        if($counter==0)
        {
            fputcsv($fp, $csvHeader);
            $counter++;
        }
        fputcsv($fp, $csvData);
        $csvData=array();
    }
    fclose($fp);
    
    function jsontocsv($data)
    {
        global $csvData,$csvHeader;
        foreach($data as $key => $value)
        {
            if(!is_array($value))
            {
                $csvData[]=$value;
                $csvHeader[]=$key;
            }
            else 
            {
                jsontocsv($value);
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置