【问题标题】:How to read and write an external local JSON file in JavaScript?如何在 JavaScript 中读写外部本地 JSON 文件?
【发布时间】:2022-08-13 01:27:43
【问题描述】:

我有一个 JSON 文件,我可以更改 \"src\" 和 \"composition\" 的值,但我无法更改操作/postrender/second 输出值的值。

我正在使用这个 js

file.template.src = \"value1\";
file.template.composition= \"value2\";
file.actions.postrender.output= \"value3\";
{
    \"template\": {
        \"src\": \"value1\",
        \"composition\": \"value2\"
    },
    \"actions\": {
        \"postrender\": [
            {
                \"module\": \"@nexrender/action-encode\",
                \"preset\": \"mp4\",
                \"output\": \"encoded.mp4\"
            },
            {
                \"module\": \"@nexrender/action-copy\",
                \"input\": \"encoded.mp4\",
                \"output\": \"d:/mydocuments/results/myresult.mp4\"
            }
        ]
    }
}
  • 那是因为postrender 不是一个对象,而是一个数组。
  • 你想改变哪个元素?

标签: javascript json


【解决方案1】:

file.actions.postrender.output= "value3"; 不起作用,因为 postrender 是一个数组。

您需要指定要修改的元素(第一个元素为 0,第二个元素为 1):

file.actions.postrender[0].output= "value3";
file.actions.postrender[1].output= "value3";

【讨论】:

    【解决方案2】:

    那是因为您没有选择数组中的第二个元素。使用索引运算符先获取第二个元素,然后修改值:

    file.actions.postrender[1].output= "value3";
    

    【讨论】:

      猜你喜欢
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-25
      • 2016-10-02
      • 1970-01-01
      相关资源
      最近更新 更多