简体   繁体   中英

Why my double quotes are received as single quotes?

I create a JSON object in my Python and I send it to my JavaScript file to display the value on a website.

I already tried to use single quotes, single quotes between double quotes

InfosThymio = {"proxh": "N/A"}

Then I send it:

def getInfos():
    global infosThymio
    return str(infosThymio)

My problem is that the JavaScript get it like this:
{'proxh':'N/A'} and I get the unexpected token ' at position 1

In the JavaScript I use this way to get the JSON and to parse it:

 getRobotData() {
        api.get()
            .then((data) => data.json())
            .then((res) => {
                const data = JSON.parse(res);
                this.setCapteur('hProxi', data.proxh);

You do not need to write const data = JSON.parse(res); because the JSON is already parsed with the line .then((data) => data.json())

The line .then((data) => data.json()) tells the computer to parse the JSON response from your API and return the resulting javascript object.

It would be more semantically correct to name your variables like this:

getRobotData() {
  api.get()
    .then((res) => res.json()) // The JSON response is parsed into data
    .then((data) => {
      // Do something with your data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM