简体   繁体   English

将javascript变量写入JSON文件

[英]Write javascript variables to JSON file

I have a an JSON file: example_json.json and this file looks like this: 我有一个JSON文件:example_json.json,该文件如下所示:

{
    "timeline":
    {
        "headline":"WELCOME",
        "type":"default",
        "text":"People say stuff",
        "startDate":"10/4/2011 15:02:00",
        "date": [
            {
                "startDate":"10/4/2011 15:10:00",
            "endDate":"10/4/2011 15:55:00",
                "headline":"prvo",
                "text":"<p>dddddddddddddddd dd</p>",
                "asset":
                {

                    "caption":"yessss"
                }
            },
            {
                "startDate":"10/4/2011 17:02:00",
                "endDate":"10/4/2011 18:02:00",
                "headline":"drugo da da",
                "text":"<p>In true political fashion, his character rattles off common jargon heard from people running for office.</p>",
                "asset":
                {
                    "media":"http://youtu.be/u4XpeU9erbg",
                    "credit":"",
                    "caption":""
                }
            }
        ]
    }
}

so this is example of json file. 因此,这是json文件的示例。

On other side I have this variables: 另一方面,我有这个变量:

var place.name;
var zajson;

and: <button>Add to timeline</button> 和: <button>Add to timeline</button>

Now I want when I click on button "add to timeline" to create new 'element' inside "date" with "startDate" to be: curentDateFormat in format month/day/year + zajson 现在,当我单击“添加到时间线”按钮以在“日期”中使用“ startDate”创建新的“ element”时,要设置为:curentDateFormat,格式为month / day / year + zajson

and to add "headline" : place.name 并添加“标题”:place.name

How I can add this variables and create new element inside "date" in example_json.json? 如何在example_json.json的“日期”中添加此变量并创建新元素?

You could do this client-side in modern browsers. 您可以在现代浏览器中执行此客户端操作。 For example you can use local storage (or cookie or something) to manipulate JSON dynamically and create a file using BlobBuilder. 例如,您可以使用本地存储(或cookie或其他东西)来动态地操作JSON并使用BlobBuilder创建文件。

Example: 例:

window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder;
var file = new window.BlobBuilder()

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

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