如何在磁盘上存储 JSON 数据?

发布于 2024-08-20 11:21:50 字数 863 浏览 3 评论 0 原文

我对 JSON 完全陌生,将来可能需要使用它,所以我阅读了一些有关它的内容。 SO 上有很多关于 JSON 的问题。我使用google找到了很多文章,我阅读了json.org,但我不明白如何存储JSON数据。

JSON 是一种轻量级数据交换格式。那么我如何存储它的数据呢?在文件中?在数据库中?有关系吗?

我可以使用它将数据传递到 jsTree (jsTree 是一个基于 javascript 的跨浏览器树组件。它是打包为 jQuery 插件。)它将与 Wordpress 一起使用。我想了解如何存储数据?在一个文件中?文本文件?在 WordPress 数据库中?哪一个更快?更好用吗?

当前状态在任何编码之前,没有应用程序运行

  • 我正在准备源数据,到目前为止我的源csv文件大小为235KB,大​​约有700行(行=未来节点) /树叶)。我使用 csv 文件只是为了收集数据,然后我将在网络服务器上上传/更新数据源。
  • 假设这个数字每周都会增加 5-10 个。
  • 该文件位于我的本地计算机上,并将(以某种方式)存储在网络托管服务器上。请注意,我将在 WordPress 中使用整个应用程序 jsTree+JSON
  • 我想我可以使用这个: 现在使用 Wordpress 解析客户端 json

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 is is a lightweight data-interchange format. So how I store its data? In a file? In a database? Does it matter?

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?

CURRENT STATUS before any coding,there is no application running

  • I am preparing the source data and so far my source csv file is 235KB in 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

殤城〤 2024-08-27 11:21:50

我想首先要理解的是 JSON 只是表示信息的一种方式。您可以按照自己喜欢的方式存储数据。如果您有一个关系数据库,您可能会想出一种来回转换数据的合理方法。

{ 
  "id": 321
  "name" : "Jim",
  "age" : 27,
  "email" : "[email protected]"
}

可能在 xml 中表示为

<person>
   <id>321</id>
   <name>Jim</name>
   <age>27</age>
   <email>[email protected]</email>
</person>

或者可能存储在看起来像 So 的表中,

_______________________________________
| id | name | age | email              |
========================================
|321 | Jim  | 27  |[email protected]     |
----------------------------------------

如果您可以根据需要存储信息。您只需要某种方法将数据序列化/反序列化为您想要的任何形式。

话虽这么说,如果您需要存储 JSON 并将其存储为文件将不起作用,您可能需要查看 CouchDBMongoDB。它们是面向文档的数据库,实际上存储 JSON 文档。它们可以让你存储任何你想要的 JSON 文档。您可以构建视图并直接查询数据,而无需将数据转换为不同的形式。

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.

{ 
  "id": 321
  "name" : "Jim",
  "age" : 27,
  "email" : "[email protected]"
}

Might be represented in xml as

<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 store the JSON and storing it as a file won't work, You probably want to look at CouchDB or 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.

画离情绘悲伤 2024-08-27 11:21:50

像 CouchDB 这样的数据库是一个将其内部存储在文件中的数据库。大多数人根本不/存储/ JSON,他们生成它并发送它,或者解析它并处理它。

JSON 是序列化数据的理想格式,但与任何其他序列化格式一样,也有同样的注意事项。您是否将 XML 存储在数据库中?通常不会。不同之处在于 XML 做出了牺牲以供人类使用,而 JSON 则做出了牺牲以易于解析和快速。

JSON 并不是 CSV 的真正替代品。将 CSV 视为松散格式的表特定转储机制。在 Excel 中导出 JSON 没有太大意义。

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.

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 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.

秋意浓 2024-08-27 11:21:50

无论您将其存储在数据库还是文件中并不重要。关键是您需要能够将其作为字符串获取(使用 HTTP 或某些服务器端脚本)。

例如,如果将其保存为名为 data.json 的文件,则可以使用 ajax 来获取它,但如果将其存储在数据库中,则需要使用某种服务器脚本(不过您仍然可以使用 ajax)。

如果您有使用 xml 的经验,只需将 json 视为相同的东西,它只是数据的字符串表示形式。

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).

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).

If you have any experience with xml just think of json as the same thing, it's just a string-representation of data.

ㄟ。诗瑗 2024-08-27 11:21:50

JSON 是一种交换格式。如果需要,您可以将其存储在文件或数据库中,就像任何其他格式一样,但这是否是一个好主意取决于您正在做什么。

你说“到目前为止,我的源 csv 文件大小为 235KB,大约有 700 行(节点/叶子)”。您是否正在考虑从 CSV 切换到 JSON? (你并没有真正说出来。)你还说“这个数字将会增加,比如说每周增加 5-10 个”。对于应用增量更改的大型文件来说,CSV 或 JSON 都不是真正的最佳选择,除非使用 CSV 可以有效地附加数据。如果您所做的只是追加,您可以坚持使用 CSV,但如果您需要进行其他修改,我可能会将数据分解到数据库中,以便可以有效地进行更新。

实际上,您所说的数据量非常小,并且每周更新的数量如此之少,您可能不需要担心效率。做任何你想做的事。 :-)

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.

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.

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. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文