具有 JSON 个数组值的 CRUD

CRUD with JSON arrays values

我正在努力创建一个带有 JSON 文件的 CRUD,但我不知道如何操作 JSON 的多个级别。所以我有以下 JSON 代码。例如,如果我想添加一个新助手,我该怎么办?这可能吗?

{
"events": [{
    "id": "1",
    "name": "Noches de trova",
    "address": "Degollado #93 Centro, 28000 Colima City",
    "city": "Colima",
    "date": "Oct 10",
    "datetime": "07:00 PM",
    "price": "0",
    "description": "Noches para escuchar, sentir y pasarla bien.",
    "image": "img/banner1.jpg",
    "publisher": "Noches café",
    "assistants": [{
        "name": "Alfredo Tomas Perez Prado",
        "address": "Las brisas #356 Col. Colinas del rey",
        "age": "29"
    }],
    "activities": [{
        "name": "Registro",
        "place": "Explanada del edificio 1",
        "time": "07:00-07:30 PM"
    }]
  }]
}

在此示例中,只需将 json 加载到变量中即可:

var data = {
"events": [{
    "id": "1",
    "name": "Noches de trova",
    "address": "Degollado #93 Centro, 28000 Colima City",
    "city": "Colima",
    "date": "Oct 10",
    "datetime": "07:00 PM",
    "price": "0",
    "description": "Noches para escuchar, sentir y pasarla bien.",
    "image": "img/banner1.jpg",
    "publisher": "Noches café",
    "assistants": [{
        "name": "Alfredo Tomas Perez Prado",
        "address": "Las brisas #356 Col. Colinas del rey",
        "age": "29"
    }],
    "activities": [{
        "name": "Registro",
        "place": "Explanada del edificio 1",
        "time": "07:00-07:30 PM"
    }]
  }]
}

data.events[0].assistants.push({name: 'John', address: '1st Street', age: 55})

console.log(data.events[0].assistants)

如果您需要通过某些 属性 查找活动,您可以使用 filter/find 找到它并添加活动。但否则只是 JS object/array 访问。

因此您可以加载 json,对其进行操作,然后将其导出。

因为JSON作为首字母缩写词意思是说是一个有效的Javascript对象。你可以用它做所有有效的 JS 操作。

For Read, evaluated the text to a javascript via JSON.parse method.

For Creating an array element. Use JS operation for push(), concat() or even inline array addition.

For Update, you could update the value of array directly as Javascript allows.

For Delete, you can use operation such as splice() or pop(), shift(). depending on what you need.

这里的关键字是 JSON 是一个 Javascript 符号对象。因此,您可以在 Javascript object/array 中使用的任何东西都可以在 JSON CRUD 中使用。