Adobe-CEP / Samples

Code samples for CEP extensions
MIT License
972 stars 551 forks source link

How to use JSON in PPRO/Premiere.jsx? ReferenceError: JSON is undefined #139

Closed zhaosheng808 closed 2 years ago

zhaosheng808 commented 2 years ago

Submit an issue

Note: issues submitted on this repo should be related to the samples contained within. If you have a general question about CEP, ExtendScript, or related topics, please visit the Extensions development forum.

Topic

This is an issue regarding:

Versions

Description of the issue

PProPanel/jsx/PPRO/Premiere.jsx when I use JSON.parse() or JSON.stringify() in Premiere.jsx it throw error: ReferenceError: JSON is undefined;

try {
  var array = [1,2,3];
  var msg = JSON.parse(array)
  $._PPP_.updateEventPanel(msg);
} catch (err) {
  $._PPP_.updateEventPanel('error:' + err);
}

image

Proposed solution or attempted solutions that failed

zhaosheng808 commented 2 years ago

JSX 并不预置 JSON对象,需要手动加载,你需要下载 json2.js并放在你的你的插件目录中,并用动态载入 JSX 文件的方法载入它。

JSX does not preset JSON objects. You need to manually download and load json2.js

premiere.jsx

function getFontsJson(){
    var fontlist = new Object(); // 创建一个要传递的对象
    fontlist.length = app.fonts.length; 
    fontlist.list = [{}];
    for (var i=0; i < app.fonts.length; i++)
    {
        fontlist.list[fontlist.list.length]=
        {
            name:app.fonts[i].name,
            style:app.fonts[i].style,
            typename:fonts[i].typename,
            postScriptName:fonts[i].postScriptName,
            family:fonts[i].family
        }
    }
    return JSON.stringify(fontlist); // 把对象转化成 JSON 字符串并返回
}

test.js

var cs = new CSInterface();
function loadJSX(fileName){
      var extensionRoot = cs.getSystemPath(SystemPath.EXTENSION) + "/jsx/";
      cs.evalScript('$.evalFile("' + extensionRoot + fileName + '")');
 }

loadJSX("json2.js"); // 为 JAX 载入 JSON 库
cs.evalScript('getFontsJson()', function (result){
  var o = JSON.parse(result); //把 JSON 字符串转换为对象
  alert(o.list[0].name);  // 可以操作得到的对象了
})