JavaScript将JSON文件中的数据存储到映射(map)中?

huangapple go评论131阅读模式
英文:

Javscript store data from json file into map?

问题

  1. 我对JavaScript还很陌生但对Java了解很多我试图通过做一些小项目来学习基础知识以便理解这门语言和代码Java我经常与将数据从映射存储在JSON文件中并在程序启动时将JSON文件加载到映射中进行交互
  2. 以下是Java的示例
  3. ```java
  4. public Map<Integer, Client> example = new HashMap<>();

以下是Client类的定义:

  1. public class Client {
  2. private String username;
  3. private String password;
  4. private String host;
  5. public Client(String username, String password, String host) {
  6. this.username = username;
  7. this.password = password;
  8. this.host = host;
  9. }
  10. public String getPassword() {
  11. return password;
  12. }
  13. public String getHost() {
  14. return host;
  15. }
  16. public String getUsername() {
  17. return username;
  18. }
  19. }

我想要在JavaScript中做同样的事情。我的映射看起来像这样:

  1. var price = new Map();

与上面的Java示例类似,我希望将这样的映射加载到一个JSON文件中,并从JSON文件中加载数据到我的映射中。

有人可以为我提供一个代码示例,演示如何将JSON中的数据存储到映射中吗?甚至提供一个教程链接也会很棒!

  1. <details>
  2. <summary>英文:</summary>
  3. I&#180;m quite new to Javascript, but know a lot about Java. Im trying to learn the basics by doing little projects, for understanding the language and code. In Java, i worked a lot with storing data from maps in json-files and, when you start the programm, the json file loads the data into the map.
  4. An example for Java:
  5. public Map&lt;Integer, Client&gt; example = new HashMap&lt;&gt;();
  6. Herefor the Client class :
  7. public class Client {
  8. private String username;
  9. private String password;
  10. private String host;
  11. public Client(String username, String password, String host) {
  12. this.username = username;
  13. this.password = password;
  14. this.host = host;
  15. }
  16. public String getPassword() {
  17. return password;
  18. }
  19. public String getHost() {
  20. return host;
  21. }
  22. public String getUsername() {
  23. return username;
  24. }
  25. }
  26. I want to do the same thing, but now in Javascript. My map looks like that:
  27. var price= new Map();
  28. Like the Java example above, I want to load such a Map into a json file and want to load the data from the json file into my map.
  29. Could somebody proivde me with a good example of code, how to store data from json in my map ? Even a link for a tutorial would be great!
  30. </details>
  31. # 答案1
  32. **得分**: 2
  33. 在JS中,使用普通对象而不是`Map()`更为常见。
  34. 例如,假设您有相同的`Client`类:
  35. ```js
  36. class Client {
  37. constructor(username, password, host) {
  38. this.username = username;
  39. this.password = password;
  40. this.host = host;
  41. }
  42. }
  43. const client1 = new Client('username1', 'password1', 'localhost');
  44. const client2 = new Client('username2', 'password2', 'localhost');

您的价格映射(从intClient)将如下所示:

  1. const price = {
  2. 1: client1,
  3. 2: client2
  4. };

现在,您可以将其序列化为JSON:

  1. const json = JSON.stringify(price);

或者从JSON解析出来:

  1. const price = JSON.parse(json);

但是,如果您真的想使用Map,这里有一个教程供您参考。

英文:

In JS it's more common to use plain objects instead of Map().
For example, let's say you have the same Client class:

  1. class Client {
  2. constructor(username, password, host) {
  3. this.username = username;
  4. this.password = password;
  5. this.host = host;
  6. }
  7. }
  8. const client1 = new Client(&#39;username1&#39;, &#39;password1&#39;, &#39;localhost&#39;);
  9. const client2 = new Client(&#39;username2&#39;, &#39;password2&#39;, &#39;localhost&#39;);

Your price map (int to Client) would look like this:

  1. const price = {
  2. 1: client1,
  3. 2: client2
  4. };

Now, you can use serialize it to json:

  1. const json = JSON.stringify(price);

Or parse it from json:

  1. const price = JSON.parse(json);

However, if you really want to use Map, here's a tutorial for it.

huangapple
  • 本文由 发表于 2020年10月11日 17:52:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64302691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定