EmEditor 不会自行将 JSON 转换为 Excel(或 CSV)文件。但是,您可以编写一个宏来将 JSON 转换为 CSV 文件。如果您在编写宏时需要支持,您可以编写一个小的 JSON 示例,以及您希望输出 CSV 的样子吗?
更新
这是一个用于将 JSON 转换为 CSV 的宏。它最多只允许 JSON 的一个深度嵌套结构。此宏检查数据中是否存在分隔符(逗号),但不检查双引号或换行代码是否存在。我还注意到你的 JSON 数据包含语法错误,我更正了它。
宏 JsonToCsv.jsee
:
function AddStr( s1, s2 )
{
if( s2.indexOf( ',' ) != -1 ) {
s2 = '"' + s2 + '"';
}
return s1 + s2;
}
sHeading = "";
sBody = "";
document.selection.SelectAll();
var json = JSON.parse(document.selection.Text);
for( property in json ) {
if( typeof json[property] === 'object' ) {
for( property2 in json[property] ) {
if( sHeading.length != 0 ){
sHeading += ",";
sBody += ",";
}
sHeading = AddStr( sHeading, property2 );
sBody = AddStr( sBody, json[property][property2] );
}
}
else {
if( sHeading.length != 0 ){
sHeading += ",";
sBody += ",";
}
sHeading = AddStr( sHeading, property );
sBody = AddStr( sBody, json[property] );
}
}
editor.NewFile();
document.selection.Text = sHeading + "\r\n" + sBody + "\r\n";
editor.ExecuteCommandByID(22528); // switch to CSV mode
输入:
{
"name": "George Washington",
"birthday": "February 22, 1732",
"first_name": "George",
"last_name": "Washington",
"birthday": "1732-02-22",
"address": {
"street_address": "3200, Mount Vernon Memorial Highway",
"city": "Mount Vernon",
"state": "Virginia",
"country": "United States"
}
}
输出:
name,birthday,first_name,last_name,street_address,city,state,country
George Washington,1732-02-22,George,Washington,"3200, Mount Vernon Memorial Highway",Mount Vernon,Virginia,United States
您可以在打开数据文件后运行此宏。为此,请将此代码另存为,例如,JsonToCsv.jsee
然后从宏菜单中的选择...中选择此文件。最后,打开您的数据文件,并在数据文件处于活动状态时选择宏菜单中的运行。