使用PHP将JSON转换为CSV格式 [英] Converting JSON to CSV format using PHP

查看:192
本文介绍了使用PHP将JSON转换为CSV格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用php脚本将json文件转换为csv格式.代码如下:

I am trying to convert a json file into csv format using a php script. The code is as follows:

if (empty($argv[1])) die("The json file name or URL is missed\n");
$jsonFilename = $argv[1];

$json = file_get_contents($jsonFilename);
$array = json_decode($json, true);
$f = fopen('output.csv', 'w');

$firstLineKeys = false;
foreach ($array as $line)
{
    if (empty($firstLineKeys))
    {
            $firstLineKeys = array_keys($line);
            fputcsv($f, $firstLineKeys);
            $firstLineKeys = array_flip($firstLineKeys);
    }

fputcsv($f, array_merge($firstLineKeys, $line));

}

这种方法有效,但仅返回JSON文件的外部变量,并收到数组到字符串转换"警告

This kind of works, but is only returning the outer variables of the JSON file, and am getting a "Array to string conversion" warning

JSON数据如下:

{"type":"NON_ATTRIBUTED","conversion":{,"value_1":"000000100355321","value_3":"XXXX","value_4":"12667","value_5":"6"},"stream_type":"COOKIE"}
{"type":"ATTRIBUTED","conversion":{,"value_1":"000000167865321","value_3":"YYYY","value_4":"12668","value_5":"0"},"stream_type":"COOKIE"}
{"type":"NON_ATTRIBUTED","conversion":{,"value_1":"000000134535321","value_3":"AAAA","value_4":"12669","value_5":"9"},"stream_type":"COOKIE"}
{"type":"NON_ATTRIBUTED","conversion":{,"value_1":"000000100357651","value_3":"WWWW","value_4":"12670","value_5":"2"},"stream_type":"COOKIE"}

我得到的输出是: 类型,转化,stream_type NON_ATTRIBUTED,数组,COOKIE NON_ATTRIBUTED,Array,COOKIE

The output I am getting is : type,conversion,stream_type NON_ATTRIBUTED,Array,COOKIE NON_ATTRIBUTED,Array,COOKIE

我期望的输出是: 类型,转化,值_1,值_3,值_4,值_5,流类型 NON_ATTRIBUTED,000000100355321,XXXX,1267、6,COOKIE ..

The output I am expecting is: type,conversion,value_1,value_3,value_4, value_5 ,stream_type NON_ATTRIBUTED,000000100355321, XXXX, 1267, 6, COOKIE ..

感谢任何帮助,因为这对我来说是很新的

ANy help appreciated as this is very new to me

推荐答案

json_decode($ json,true);将JSON对象转换为关联数组.所以这个

json_decode($json, true); converts JSON objects to associative arrays. So this

{
    "type":"NON_ATTRIBUTED",
    "conversion":{,
        "value_1":"000000100355321",
        "value_3":"XXXX",
        "value_4":"12667",
        "value_5":"6"
    },
    "stream_type":"COOKIE"
}

成为这个:

array(3) { 
    ["type"]=> string(14) "NON_ATTRIBUTED" 
    ["conversion"]=> array(4) { 
        ["value_1"]=> string(15) "000000100355321" 
        ["value_3"]=> string(4) "XXXX" 
        ["value_4"]=> string(5) "12667" 
        ["value_5"]=> string(1) "6" 
    } 
    ["stream_type"]=> string(6) "COOKIE" 
}

如您所见,存在嵌套数组.然后,您尝试使用此行将数组的所有元素插入文本文件(csv只是一个简单的文本文件):

As you see there is nested arrays. And you trying to insert all elements of array to your text file (csv is just a simple text file) with this line:

fputcsv($f, array_merge($firstLineKeys, $line));

当array的元素为string时,效果很好.但是当元素是数组时,我们得到了数组到字符串的转换.因此,必须在嵌套数组上使用loop或array_merge来防止这种情况.

It works nice when element of array is string. But when the element is array we got the Array to string conversion. So you must to use loop or array_merge on a nested array to prevent this.

我不清楚您的csv的外观如何,但希望此代码修复可以对您有所帮助.如果没有,请在下面写评论.

I can't clearly understand how your csv must look like, but I hope this fix of your code will help you. If not, write a comment below.

if (empty($argv[1])) die("The json file name or URL is missed\n");
$jsonFilename = $argv[1];

$json = file_get_contents($jsonFilename);
$array = json_decode($json, true);
$f = fopen('output.csv', 'w');

$firstLineKeys = false;
foreach ($array as $line)
{
    if (empty($firstLineKeys))
    {
        $firstLineKeys = array_keys($line);
        fputcsv($f, $firstLineKeys);
        $firstLineKeys = array_flip($firstLineKeys);
    }
    $line_array = array($line['type']);
    foreach ($line['conversion'] as $value)
    {
        array_push($line_array,$value);
    }
    array_push($line_array,$line['stream_type']);
    fputcsv($f, $line_array);

}

您的json中也有一个错误-不必要的逗号:"conversion":{,

There is also a mistake in your json - unneeded comma: "conversion":{,

这篇关于使用PHP将JSON转换为CSV格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆