在JavaScript中使用JSON文件永久存储小型数据库 [英] Using a JSON file to store a small database persistently, in javascript

查看:120
本文介绍了在JavaScript中使用JSON文件永久存储小型数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JSON的新手.

I am new to the use of JSON.

我希望我的网页在一个表中显示一个包含数百条记录的小型数据库.为了避免将数据放入MySQL数据库或类似数据库中的麻烦,我认为我将从JSON文件中读取数据,然后将其写到JSON文件中,这将构成对我的方便,持久的存储我网站上的数据库.

I want my webpage to display, in a table, a small database of a few hundred records. Wanting to avoid the hassle of putting my data in a MySQL database or something similar, I figured I would read my data from a JSON file, and write it out to a JSON file, and that this would amount to convenient, persistent storage of my database at my website.

因此,我花了一些时间编写一个脚本,将现有的论文文件转换为包含我所有记录的papers.json文件.看起来像:

So I spent a bit of time writing a script that translated my existing papers file into a papers.json file that contains all my records. It looks like:

[
  {"title" :  "IEEE Standard for Local and Metropolitan Area Networks: Overview and Architecture",
   "authors" :  "IEEE",
   "pub" :  "IEEE 802-2001 standard",
   "datepub" :  "2001",
   "keywords" :  "MAC",
   "dateread" :  "200309",
   "physloc" :  "box i",
   "comment" :  "Indicates how you can manage addresses assigned to you by IEEE."
  },
  {"title" :  "A framework for delivering multicast messages in networks with mobile hosts",
   "authors" :  "A. Acharya, B. R. Badrinath",
   "pub" :  "Mobile Networks and Applications v1 pp 199-219",
   "datepub" :  "1996",
   "keywords" :  "multicast mobile MH MSS",
   "dateread" :  "",
   "physloc" :  "box a",
   "comment" :  ""
  },

    <hundreds more similar papers records here...>

  },
  {"title" :  "PiOS: detecting privacy leaks in iOS applications",
   "authors" :  "M. Egele, C. Kruegel, E. Kirda, G. Vigna",
   "pub" :  "NDSS 2011",
   "datepub" :  "2011",
   "keywords" :  "iOS app location leakage",
   "dateread" :  "",
   "physloc" :  "box e",
   "comment" :  "discussed at Latte"
  }
]

这是我用来阅读的javascript代码. (我尚未对记录的写出进行编码,因为读取无法正常进行.)

Here is the javascript code I am using to read it. (I haven't yet coded the write-out of the records, because the read isn't working.)

var pdb = []; // global

var doneReading = false; //global

$(document).ready(function() {
        $.getJSON('papers.json',function(data) {
            pdb = data;
            doneReading = true;
        });

        while (!doneReading) {}

        alert("finished assignment of JSON to pdb"+" "+typeof pdb); 
        //alert(pdb[0].title);
        console.log(pdb[2]);
        //setup();
});

该脚本永远挂在while循环中.为什么?

The script hangs in the while loop forever. Why?

我也是jQuery的新手.如果不使用图书馆,我会感到更自在,一次新事物对我来说足够了.没有jQuery,可以轻松地操作JSON文件吗?

Also I am new to jQuery. I would feel more comfortable doing this without the use of a library, one new thing at a time being enough for me. Can one manipulate JSON files easily without jQuery?

推荐答案

常规浏览器JavaScript是单线程的.正是因为无限while永不结束,JavaScript引擎也从未处理过$.getJSON调用的成功回调.浏览器唯一的JS线程会永远循环,永远不会继续执行回调.

Normal browser JavaScript is single-threaded. Precisely because the infinite while never ends, the JavaScript engine never gets to process the success callback of the $.getJSON call. Your browser's sole JS thread sits looping forever and never moves on to the callback.

解决方案:您应该消除循环并将当前在无限循环之后的代码移到您的$.getJSON回调中.

Solution: You should eliminate the loop and move code that is currently after the infinite loop into your $.getJSON callback.

这篇关于在JavaScript中使用JSON文件永久存储小型数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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