使用javascript更新json文件

时间:2021-07-15 05:22:43

I'd like to preface this by saying I know what I'm doing isn't ideal. With that covered:

在此之前,我想说我知道我所做的并不理想。与覆盖:

I need to update data in a JSON file via javascript. Here is the JSON file as it currently stands:

我需要通过javascript在JSON文件中更新数据。以下是当前的JSON文件:

{
"nextid" : 3,
"ingredients" : [
{
    "id": 1,
    "name": "onion",
    "kilojoules": 180,
    "quantity": 1,
    "measurement": "whole",
    "nutrition": [
        {"value": "Fat Total", "quantity": 110},
        {"value": "Saturated Fat", "quantity": 46},
        {"value": "Sodium", "quantity": 4},
        {"value": "Carbohydrates Total", "quantity": 10000},
        {"value": "Sugar", "quantity": 5000},
        {"value": "Fibre", "quantity": 2000},
        {"value": "Proten", "quantity": 1000}
    ]},

{
    "id" : 2,
    "name": "carrot",
    "kilojoules": 56,
    "quantity": 1,
    "measurement": "whole",
    "nutrition": [
        {"value": "Fat Total", "quantity": 66},
        {"value": "Saturated Fat", "quantity": 11},
        {"value": "Sodium", "quantity": 26},
        {"value": "Carbohydrates Total", "quantity": 3000},
        {"value": "Sugar", "quantity": 2000},
        {"value": "Fibre", "quantity": 1000},
        {"value": "Proten", "quantity": 279}
    ]}
]

}

Now let's say I want to edit the carrot object. I've built an updated carrot object on my html page and have it sitting as an object in the javascript. What am I going to need to do to update the source json with my edits?

现在假设我想编辑胡萝卜对象。我在html页面上构建了一个更新的胡萝卜对象,并将其作为javascript中的对象。用我的编辑来更新源json需要做什么?

Like I said before, I know this isn't ideal. I should have used a database for storing and manipulating data, but I've made my bed now and I have to get this working, regardless of how much it might make you all cringe.

就像我之前说的,我知道这并不理想。我本应该使用一个数据库来存储和操作数据,但我现在已经整理好了我的床,我必须让它工作,不管它会让你们感到多么的害怕。

Research tells me I'm going to need to use PHP or ASP on the server side to collect the parameters the javascript passes to it, but I have no idea where to begin with that.

研究告诉我,我需要在服务器端使用PHP或ASP来收集javascript传递给它的参数,但是我不知道从哪里开始。

I'm working in visual studio 2012 and the parameters of the project prohibit me from using addons. NuGet code libraries yes, but no addons. On that basis, I think it means I can't use PHP. Am I correct in my thinking there?

我在visual studio 2012工作,项目的参数禁止我使用addons。NuGet代码库是,但是没有插件。基于此,我认为这意味着我不能使用PHP。我的想法对吗?

Note: Yes it's for an assignment, but altering the json file is beyond the scope of the requirements. Being able to process json data dynamically is enough for the project, I'd just REALLY like to be able to put some back.

注意:是的,它是用于赋值的,但是修改json文件超出了需求的范围。能够动态地处理json数据对于这个项目来说已经足够了,我非常希望能够放一些回去。

Thanks in advance

谢谢提前

EDIT: Probably should have shared this too. This is the javascript that opens the json file. The result of the opening is stored in a script wide variable called ingJson.

编辑:可能也应该分享一下。这是打开json文件的javascript。打开的结果存储在一个名为ingJson的脚本宽变量中。

function downloadIngredients() {
$(document).ready(function () {
    $.getJSON("/data/ingredientsInfo.js", function (result) {
        try
        {
            ingJson = result;
            ingredients = result.ingredients;
            ingredients.sort(orderByNameAscending);
            buildListBox(ingredients);
        }
        catch (err) {
            alert(err.message);
        }
    });
});
}

1 个解决方案

#1


3  

To edit a JSON file:

编辑JSON文件:

  1. Open the file with php
  2. 使用php打开文件
  3. Send the contents to the browser (Just dump it as string in a variable in a script tag)
  4. 将内容发送到浏览器(只需将其作为字符串转储到脚本标记中的变量中)
  5. Parse the JSON with JavaScript (onload -> get the string from the script tag)
  6. 使用JavaScript解析JSON (onload ->从脚本标记获取字符串)
  7. Edit the object
  8. 编辑的对象
  9. Convert it to a JSON string again
  10. 再次将其转换为JSON字符串
  11. Send it back to php
  12. 发送回php
  13. Have php overwrite the file.
  14. 让php覆盖文件。

It's a shame you can't just edit the file directly in php, that'd render steps 2-6 unnecessary, you'd just parse, edit, and encode the JSON in php.

遗憾的是,您不能直接在php中编辑文件,这将使步骤2-6变得不必要,您只需在php中解析、编辑和编码JSON。

Edit: Since you seem to have the data in JavaScript already, steps 1-3 are accounted for.

编辑:因为您似乎已经有了JavaScript中的数据,所以步骤1-3已经说明了。

#1


3  

To edit a JSON file:

编辑JSON文件:

  1. Open the file with php
  2. 使用php打开文件
  3. Send the contents to the browser (Just dump it as string in a variable in a script tag)
  4. 将内容发送到浏览器(只需将其作为字符串转储到脚本标记中的变量中)
  5. Parse the JSON with JavaScript (onload -> get the string from the script tag)
  6. 使用JavaScript解析JSON (onload ->从脚本标记获取字符串)
  7. Edit the object
  8. 编辑的对象
  9. Convert it to a JSON string again
  10. 再次将其转换为JSON字符串
  11. Send it back to php
  12. 发送回php
  13. Have php overwrite the file.
  14. 让php覆盖文件。

It's a shame you can't just edit the file directly in php, that'd render steps 2-6 unnecessary, you'd just parse, edit, and encode the JSON in php.

遗憾的是,您不能直接在php中编辑文件,这将使步骤2-6变得不必要,您只需在php中解析、编辑和编码JSON。

Edit: Since you seem to have the data in JavaScript already, steps 1-3 are accounted for.

编辑:因为您似乎已经有了JavaScript中的数据,所以步骤1-3已经说明了。