简体   繁体   English

使用javascript更新json文件

[英]Use javascript to update a json file

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. 我需要通过javascript更新JSON文件中的数据。 Here is the JSON file as it currently stands: 这是目前的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. 我在我的html页面上构建了一个更新的胡萝卜对象,并将其作为javascript中的对象。 What am I going to need to do to update the source json with my edits? 我需要做些什么才能使用我的编辑来更新源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. 我在visual studio 2012工作,项目的参数禁止我使用插件。 NuGet code libraries yes, but no addons. NuGet代码库是的,但没有插件。 On that basis, I think it means I can't use PHP. 在此基础上,我认为这意味着我不能使用PHP。 Am I correct in my thinking there? 我的想法是否正确?

Note: Yes it's for an assignment, but altering the json file is beyond the scope of the requirements. 注意:是的,它用于分配,但是更改json文件超出了要求的范围。 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数据对于项目来说已经足够了,我真的很想能够放回一些。

Thanks in advance 提前致谢

EDIT: Probably should have shared this too. 编辑:可能也应该分享这个。 This is the javascript that opens the json file. 这是打开json文件的javascript。 The result of the opening is stored in a script wide variable called ingJson. 打开的结果存储在名为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);
        }
    });
});
}

To edit a JSON file: 要编辑JSON文件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM