删除外部.json对象并再次保存

时间:2017-09-16 12:07:03

标签: javascript jquery json

我有一个资源列表,例如从我从同一服务器外部加载到我的应用程序的 resources_data.json 数据加载的以下html内容。

"calendarResources": [
    {
      "id": 1,
      "resourceName": "Demo"
    },
    {
      "id": "4",
      "resourceName": "New Test Resource"
    },
    {
      "id": "2",
      "resourceName": "test"
    },
    {
      "id": "5",
      "resourceName": "another test"
    },
    {
      "id": "6",
      "resourceName": "new test data"
    },
    {
      "id": "7",
      "resourceName": "better one"
    },
    {
      "id": "8",
      "resourceName": "the one!"
    },
    {
      "id": "12",
      "resourceName": "Dune one"
    },
    {
      "id": "13",
      "resourceName": "res test"
    }
  ]

我希望能够通过“id”删除此列表中的项目,而不必匹配“resourceName”,因此对于每次删除,我将删除整个对象本质上我不想将文件保存回服务器以节省响应的时间,但我不确定是否可以删除外部文件的字符串而不重新保存它。

我的JSON:

function deletingResource() {
        $("body").on("click", ".deleteResource", function (e) {
            e.preventDefault();

            var resourceId = $(this).data("id");
            var resourceName = $(this).data("name");

            $.getJSON(appDirLocation + "_data/resources_data.json", function (jsonData) {

                console.log(jsonData);

                var resourceDeletion = ({
                    "id": resourceId,
                    "resourceName": resourceName
                });

                delete jsonData.calendarResources[resourceDeletion]; // delete data string


                var newJsonOutput = JSON.stringify(jsonData); //stringify new data to save

                var jsonFile = new BCAPI.Models.FileSystem.File(appDirLocation + "_data/resources_data.json");

                jsonFile.upload(newJsonOutput).done(function () {
                    $("#resourcesList").html("");
                    console.log("RESOURCE DATA DELETED");
                    renderResources();
                }).error(function (jqXHR) {
                    console.log("RESOURCES JSON FAILED DELETE: " + jqXHR.responseText);
                }); // END OF JSON CREATING

                console.log(newJsonOutput);

            }).done(function () {
                $(this).closest(".resourcesData").hide();
            }).fail(function (jqXHR) {
                console.log("Request failed." + "Error code: " + jqXHR.status + "Error text: " + jqXHR.statusText + "Response text: " + jqXHR.responseText);
            });

        });

    } // DELETING RESOURCES
    deletingResource();

我正在尝试使用删除函数来删除字符串并再次将数据上传回json,但这里出现了一些错误,即没有任何作用,我再次得到相同的数据。

MY JS:

function slashEscape(contents) {
    return contents
        .replace(/\\/g, '\\\\')
        .replace(/"/g, '\\"')
        .replace(/\n/g, '\\n');
}

var replacements = {'\\\\': '\\', '\\n': '\n', '\\"': '"'};

function slashUnescape(contents) {
    return contents.replace(/\\(\\|n|")/g, function(replace) {
        return replacements[replace];
    });
}

var tests = [
    '\\', '\\\\', '\n', '\\n', '\\\n', '\\\\n',
    '\\\\\n', '\\\\\\n', '\\"\\\\n', '\n\n',
    '\n\n\n', '\\n\n', '\n\\n', '\\n\\n',
    '\\\n\\n\nn\n\\n\\\n\\\\n', '"', '\\"', '\\\\"'
];

tests.forEach(function(str) {
    var out = slashUnescape(slashEscape(str));
    
    // assert that what goes in is what comes out
    console.log(str === out, '[' + str + ']', '[' + out + ']');
});

先谢谢你的帮助......

1 个答案:

答案 0 :(得分:0)

根据已回复的问题here也引用了 Array.prototype.filter()函数,我能够优雅地解决这个问题!

我推荐给任何希望使用javascript工作json文件来检查MDN WEB DOCS

中javascript库的Array属性的人

代码已修复:

jsonData.calendarResources = jsonData.calendarResources.filter(item => item.id != resourceId); // WORKING ######

var newJsonOutput = JSON.stringify(jsonData);
var jsonFile = new BCAPI.Models.FileSystem.File(appDirLocation + "_data/resources_data.json");