English 中文(简体)
使用 JSON 文件 持续存储一个小数据库, 以 Javaramp 格式保存
原标题:Using a JSON file to store a small database persistently, in javascript

我对JSON的用途很新奇

我希望我的网页能在一个表格中显示一个包含几百个记录的小数据库。 为了避免将我的数据放入 MySQL 数据库或类似数据库的麻烦,我想我会从JSON 文件中读取我的数据,并将其写进JSON 文件中,这等于方便、持续地将我的数据库储存在我的网站上。

所以我花了一点时间 写一个脚本 将我现有的文件文件文件 翻译成一个包含我所有记录的文件。它看起来像:

[
  {"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"
  }
]

这是我用来读的刺绣代码。 (我还没有编码记录中的写法, 因为读法不起作用 。 )

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();
});

剧本会永远挂在脚本上 为什么?

我对JQuery也是新手。 不使用图书馆,我就会觉得这样做比较舒服,因为图书馆是一个新事物,现在对我来说足够了。 一个人可以轻而易举地操作JSON文件而不用jQuery吗?

最佳回答

普通浏览器 JavaScript 是单行的。 确切地说, < em> 因为 < / em> 无限 < code> 而 永远不会结束, JavaScript 引擎永远无法处理 < code>$. getJson 呼叫的成功回调。 您的浏览器唯一的 JSON < /code > 线索会永远循环, 永远也不会移动到回调上 。

<强度 > 解决方案 : 您应该删除当前在无限循环后进入您 < code>$. getJSON 回调的循环和移动代码 。

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签