CSV形式からデータを取得してJSON形式に変換。
以下みたいなCSVを用意↓
aa,bb,cc ddd,eee,fff ggg,hhh,ii
スクリプト内容↓
filename = File.openDialog("ファイルを選択してください",""); fileObj = new File(filename); if (fileObj.open("r")){ var b = new Object; while(!fileObj.eof){ txt = fileObj.readln(); txt = txt.split(","); var o = new Object; o.a = txt[1]; o.b = txt[2]; b[ txt[0] ] = o; } } for(tmp in b){ $.writeln ("name:" + tmp); $.writeln ("a:" + b[tmp].a); $.writeln ("b:" + b[tmp].b); }
15/06/08追加
CSVの一行目を変数名にしObjectを作成。関数化。
/* csv2Object(csvPath) ex: filename = File.openDialog("ファイルを選択してください",""); data = csv2Object(filename); $.writeln ( data.toSource() ); */ function csv2Object(csvPath){ fileObj = new File(filename); if (fileObj.open("r")){ var lastObj = new Object; num = 0; while(!fileObj.eof){ if(num != 0){ txt = fileObj.readln(); txt = txt.split(","); var tmpObj = new Object; i = 1; while (i < Nametxt.length) { tmpObj[Nametxt[i]] = txt[i]; i+=1; } lastObj[ txt[0] ] = tmpObj; } else{ Nametxt = fileObj.readln(); Nametxt = Nametxt.split(","); } num += 1; } } fileObj.close(); return lastObj; }
参考リンク
After Effects スクリプト | JSON風に設定ファイルなどを保存するなら。
Excelで入力された座標データを利用する