将javascript变量写入JSON文件

时间:2013-09-22 22:34:52

标签: javascript jquery json variables jsonp

我有一个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":""
                }
            }
        ]
    }
}

所以这是json文件的例子。

另一方面,我有这些变量:

var place.name;
var zajson;

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

现在我希望当我点击“添加到时间轴”按钮在“日期”内创建新的“元素”时,“startDate”为:curentDateFormat,格式为月/日/年+ zajson

并添加“标题”:place.name

如何添加此变量并在example_json.json中的“date”中创建新元素?

1 个答案:

答案 0 :(得分:0)

您可以在现代浏览器中执行此客户端操作。例如,您可以使用本地存储(或cookie或其他)动态操作JSON并使用BlobBuilder创建文件。

示例:

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