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

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

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

An example for Java: 

    public Map&lt;Integer, Client&gt; example = new HashMap&lt;&gt;();
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');

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

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(&#39;username1&#39;, &#39;password1&#39;, &#39;localhost&#39;);
const client2 = new Client(&#39;username2&#39;, &#39;password2&#39;, &#39;localhost&#39;);

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.

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:

确定