英文:
Javscript store data from json file into map?
问题
我对JavaScript还很陌生,但对Java了解很多。我试图通过做一些小项目来学习基础知识,以便理解这门语言和代码。在Java中,我经常与将数据从映射存储在JSON文件中并在程序启动时将JSON文件加载到映射中进行交互。
以下是Java的示例:
```java
public Map<Integer, Client> example = new HashMap<>();
以下是Client类的定义:
public class Client {
private String username;
private String password;
private String host;
public Client(String username, String password, String host) {
this.username = username;
this.password = password;
this.host = host;
}
public String getPassword() {
return password;
}
public String getHost() {
return host;
}
public String getUsername() {
return username;
}
}
我想要在JavaScript中做同样的事情。我的映射看起来像这样:
var price = new Map();
与上面的Java示例类似,我希望将这样的映射加载到一个JSON文件中,并从JSON文件中加载数据到我的映射中。
有人可以为我提供一个代码示例,演示如何将JSON中的数据存储到映射中吗?甚至提供一个教程链接也会很棒!
<details>
<summary>英文:</summary>
I´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.
An example for Java:
public Map<Integer, Client> example = new HashMap<>();
Herefor the Client class :
public class Client {
private String username;
private String password;
private String host;
public Client(String username, String password, String host) {
this.username = username;
this.password = password;
this.host = host;
}
public String getPassword() {
return password;
}
public String getHost() {
return host;
}
public String getUsername() {
return username;
}
}
I want to do the same thing, but now in Javascript. My map looks like that:
var price= new Map();
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.
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!
</details>
# 答案1
**得分**: 2
在JS中,使用普通对象而不是`Map()`更为常见。
例如,假设您有相同的`Client`类:
```js
class Client {
constructor(username, password, host) {
this.username = username;
this.password = password;
this.host = host;
}
}
const client1 = new Client('username1', 'password1', 'localhost');
const client2 = new Client('username2', 'password2', 'localhost');
您的价格映射(从int
到Client
)将如下所示:
const price = {
1: client1,
2: client2
};
现在,您可以将其序列化为JSON:
const json = JSON.stringify(price);
或者从JSON解析出来:
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:
class Client {
constructor(username, password, host) {
this.username = username;
this.password = password;
this.host = host;
}
}
const client1 = new Client('username1', 'password1', 'localhost');
const client2 = new Client('username2', 'password2', 'localhost');
Your price map (int
to Client
) would look like this:
const price = {
1: client1,
2: client2
};
Now, you can use serialize it to json:
const json = JSON.stringify(price);
Or parse it from json:
const price = JSON.parse(json);
However, if you really want to use Map
, here's a tutorial for it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论