在本地保存JSON文件 [英] Save JSON File Locally

查看:154
本文介绍了在本地保存JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个HTML5网站,允许您创建和定位具有多个属性的形状。形状创建为class =shape的div,并使用data方法将属性存储到每个形状div。我希望能够在JSON文件中使用用户定义的名称保存形状的位置和属性。

I am designing an HTML5 website that allows you to create and position shapes with several attributes. The shapes are created as divs of class="shape" and the attributes are stored to each shape div using the data method. I'd like to be able to save the position and attributes of the shapes, in a JSON file, with a user defined name.

目前,我正在使用ajax和php生成JSON文件并将其保存在服务器上。

Currently, I am using ajax and php to generate the JSON file and save it on the server.

关于应如何做的任何想法或想法?库,API和示例的加分点!

Any ideas or thoughts on how it should be done? Bonus points for libraries, APIs and examples!

澄清编辑:最好保存到用户定义位置的离散文件。但是,根据两个选项之间的难度因素,浏览器存储肯定就足够了。

Clarification Saving to a discrete file in a user-defined location would be preferable. However, depending on the difficulty factor between the two options, browser storage could certainly suffice.

推荐答案

我想有很多可能性,但一个是使用本地存储并与您当前的应用程序构建某种同步。您可以将它与ajax或websockets等同步。

I guess there are a lot of possibilities, but one is to use local storage and build some kind of syncronization with your current application. You can sync it with ajax or websockets or so on.

var updateLocalData = function(data){
    if( supports_html5_storage() ){
        localStorage.setItem('shape', JSON.stringify(data))
    }
}

同步你的数据得到书面的localStorage东西并用你喜欢的方法发送它。并且还要记住使用任何类型的时间戳始终能够找到最新版本。

to sync your data get the written localStorage stuff and send it with your prefered method. And also keep in mind to use any kind of timestamp to always be able to find the latest version.

var getLocalData = function(){
    if( supports_html5_storage() ){
        var userData = JSON.parse(localStorage.getItem('shape'));
        if(userData !== null){
            return userData;
        }
    }
    var tStamp = getCurrentTimeStamp()+"";
    var obj = {};
    obj[tStamp] = {"shapedata": ... };
    return obj;
}

这篇关于在本地保存JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆