PHP库将JSON转换为CSV吗? [英] PHP Library to convert JSON to CSV?
本文介绍了PHP库将JSON转换为CSV吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个JSON服务,需要创建一个脚本以将数据导出到CSV文件.有没有人建议您将JSON迁移为CSV格式的方法或库?
I've got a JSON service and need to create a script to export data to CSV files. Does anyone have a method or library you can suggest to migrate JSON to CSV format?
这是一个示例格式,尽管我希望必须对解决方案进行改造才能使用它:
Here's an example format though I expect to have to retro-fit the solution to work with it:
{"service_name":
{ key : value, key : value....}
}
或:
{"service_name":
[
{ key : value, key : value....},
...
]
}
推荐答案
我通常同意注释者的意见,但是如果您是通过这种方式准备数据的,那么您所需的不是伪代码吗?
i generally agree with the commenters, but if you're data is prepared this way, isn't this pseudo-code all you need?
$json_str = "{'aintlist':[4,3,2,1], 'astringlist':['str1','str2']}";
$json_obj = json_decode ($json_str);
$fp = fopen('file.csv', 'w');
foreach ($json_obj as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
这篇关于PHP库将JSON转换为CSV吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文