Javascript 如何将 JSON 数据存储在磁盘上?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2202645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I store JSON data on a disk?
提问by Radek
I am totally new to JSON and I might need to use it in the future so I did some reading bout it. There's lots of questions regarding JSON on SO. I found heaps of articles using google, I read json.org but I did not understand how to store JSON data.
我对 JSON 完全陌生,将来可能需要使用它,所以我阅读了一些内容。关于 SO 上的 JSON 有很多问题。我使用谷歌找到了大量文章,我阅读了 json.org 但我不明白如何存储 JSON 数据。
JSON is is a lightweight data-interchange format. So how I store its data? In a file? In a database? Does it matter?
JSON 是一种轻量级的数据交换格式。那么我如何存储它的数据呢?在一个文件中?在数据库中?有关系吗?
I can use it to pass the data to jsTree(jsTree is a javascript based, cross browser tree component. It is packaged as a jQuery plugin.) It would be with Wordpress. I am trying to understand how I will store the data? In a file? Text file? In Wordpress database? Which one is faster? Better to use?
我可以用它来将数据传递给jsTree(jsTree 是一个基于 javascript 的跨浏览器树组件。它被打包为一个 jQuery 插件。)它将与 Wordpress 一起使用。我想了解我将如何存储数据?在文件中?文本文件?在 Wordpress 数据库中?哪个更快?更好用吗?
CURRENT STATUSbefore any coding,there is no application running
任何编码前的当前状态,没有应用程序正在运行
- I am preparing the source data and so far my source csv file is 235KBin size with about 700lines (line = future nodes/leaves). I use csv file just to collect data then I will upload/update the data source on the web server.
- The number is going to grow let's say every week by 5-10.
- The file is on my local computer and will be stored (somehow) on a webhosting server. Please note that I will use the whole application jsTree+JSON within Wordpress
- I guess I can use this: Now parse Client side json with Wordpress
- 我正在准备源数据,到目前为止,我的源 csv 文件大小为 235KB,大约有 700 行(行 = 未来节点/叶子)。我使用 csv 文件只是为了收集数据,然后我将上传/更新网络服务器上的数据源。
- 比方说每周增加 5-10 个。
- 该文件在我的本地计算机上,并将(以某种方式)存储在虚拟主机服务器上。请注意,我将在 Wordpress 中使用整个应用程序 jsTree+JSON
- 我想我可以使用这个:现在用 Wordpress 解析客户端 json
回答by Steve g
I guess the first thing to understand is that JSON is just one way of representing information. You can store the data however you like. If you have a relational database, you can probably come up with a reasonable way of converting the data back and forth.
我想首先要了解的是 JSON 只是表示信息的一种方式。您可以随意存储数据。如果您有一个关系数据库,您可能会想出一种来回转换数据的合理方法。
{
"id": 321
"name" : "Jim",
"age" : 27,
"email" : "[email protected]"
}
Might be represented in xml as
可能在 xml 中表示为
<person>
<id>321</id>
<name>Jim</name>
<age>27</age>
<email>[email protected]</email>
</person>
Or might be stored in the a table that looks like
或者可能存储在一个看起来像的表中
_______________________________________
| id | name | age | email |
========================================
|321 | Jim | 27 |[email protected] |
----------------------------------------
So if you can store the information however you want. You just need some way to serialize/unserialize the data into whatever form you want.
因此,如果您可以根据需要存储信息。您只需要某种方式将数据序列化/反序列化为您想要的任何形式。
All that being said, If you need to storethe JSON and storing it as a file won't work, You probably want to look at CouchDBor MongoDB. They are document-oriented databases that actually store JSON documents. They will let you store whatever JSON documents you want. You can build views and and query the data directly without having to convert the data to different forms.
话虽如此,如果您需要存储JSON 并将其存储为文件将不起作用,您可能想查看CouchDB或MongoDB。它们是实际存储 JSON 文档的面向文档的数据库。他们会让你存储任何你想要的 JSON 文档。您可以直接构建视图和查询数据,而无需将数据转换为不同的形式。
回答by Evan Carroll
Somethings like CouchDB are a database that store it internally in a file. Most people don't /store/ JSON at all, they generate it and send it, or parse it and process it.
像 CouchDB 这样的东西是一个将其内部存储在文件中的数据库。大多数人根本不 /store/ JSON,他们生成并发送它,或者解析它并处理它。
JSON is an ideal format for serializing data, but the same caveats apply to it as any other serialization format. Do you store XML in a DB? Usually not. The difference being XML makes sacrifices to include humans use, and JSON makes sacrifices to be easily parseable and fast.
JSON 是序列化数据的理想格式,但与任何其他序列化格式相同的警告也适用于它。您是否将 XML 存储在数据库中?通常不会。不同之处在于 XML 牺牲了人类使用,而 JSON 牺牲了易于解析和快速。
JSON is not really a replacement for a CSV. Think of a CSV as loosely-formated table specific dumping mechanism. It wouldn't make too much sense to have a JSON export in excel.
JSON 并不能真正替代 CSV。将 CSV 视为格式松散的表特定转储机制。在 excel 中导出 JSON 并没有太大意义。
回答by Alxandr
Weather you store it in a database or in a file doesn't really matter. The point is that you need to be able to fetch it as a string (using HTTP or some server-side-script).
您将其存储在数据库或文件中的天气并不重要。关键是您需要能够将其作为字符串获取(使用 HTTP 或某些服务器端脚本)。
For instance if you save it as a file named data.json you could use ajax to fetch it, but if you store it in a database you need to use some kind of server-scripting (you could still use ajax though).
例如,如果你将它保存为一个名为 data.json 的文件,你可以使用 ajax 来获取它,但是如果你将它存储在数据库中,你需要使用某种服务器脚本(尽管你仍然可以使用 ajax)。
If you have any experience with xml just think of json as the same thing, it's just a string-representation of data.
如果您对 xml 有任何经验,只需将 json 视为同一件事,它只是数据的字符串表示形式。
回答by Laurence Gonsalves
JSON is an interchange format. You can store it in a file or a DB if you want, just like any other format, though whether that's a good idea depends on exactly what you're doing.
JSON 是一种交换格式。如果需要,您可以将它存储在文件或数据库中,就像任何其他格式一样,尽管这是否是一个好主意取决于您在做什么。
You say "So far my source csv file is 235KB in size with about 700lines (nodes/leaves)". Are you considering switching from CSV to JSON? (You don't really say.) You also say "The number is going to grow let's say every week by 5-10". Neither CSV or JSON are really optimal for large files that will have incremental changes applied, except with CSV you can append data efficiently. If appending is all you're doing you could stick with CSV, but if you need to do other modifications, I'd probably decompose the data into a DB so that updates could be made efficiently.
您说“到目前为止,我的源 csv 文件大小为 235KB,大约有 700 行(节点/叶子)”。您是否正在考虑从 CSV 切换到 JSON?(你并没有真正说。)你还说“这个数字会增加,比如说每周增加 5-10 个”。对于将应用增量更改的大文件,CSV 或 JSON 都不是真正的最佳选择,除非使用 CSV 可以有效地附加数据。如果追加就是你所做的一切,你可以坚持使用 CSV,但如果你需要进行其他修改,我可能会将数据分解到一个数据库中,以便可以有效地进行更新。
Actually, the amount of data you're talking about is pretty small, and with such a small number of updates per week, you probably don't need to worry about efficiency. Do whatever you want. :-)
其实你说的数据量是很小的,每周更新这么少,你大概不用担心效率问题。做你想做的。:-)

