简体   繁体   English

JSON 解析器在 javascript 中为对象键添加双引号

[英]JSON parser adds double quotes to object key in javascript

I have encountered a weird problem.我遇到了一个奇怪的问题。 I am consuming a RESTful API which returns the following response body:我正在使用一个 RESTful API,它返回以下响应正文:

{
"bourseMarket":[2.9966187229626504E16,2.8923052836886444E16],
"bourseTrade":[1.13589172355139E14,4.903299930043E13],
"‌bourseMarketf":[6.037694517655877E15,5.876172638826381E15],
"‌bourseTradef":[4.3646660180697E13,2.2708648590401E13]
}

When I parse this, JS returns the following object:当我解析这个时,JS 返回以下对象:

bourseMarket: Array [ 29966187229626504, 28923052836886444 ]
​bourseTrade: Array [ 113589172355139, 49032999300430 ]
​"‌bourseMarketf": Array [ 6037694517655877, 5876172638826381 ]
​"‌bourseTradef": Array [ 43646660180697, 22708648590401 ]

As you may see, JS adds double quotes to third and fourth object keys.如您所见,JS 为第三个和第四个对象键添加了双引号。 And after that I can not refer to the object key and value:之后我无法引用对象键和值:

TypeError: obj.bourseTradef is undefined

I even tried obj["bourseTradef"] but was unsuccessful too.我什至尝试过obj["bourseTradef"]但也没有成功。

I use standard methods to retrieve data:我使用标准方法来检索数据:

const getData = async (url) => {

    return await fetch(url).then(resp => {
        if (resp.status === 200) {

            return resp.json();
        }
        else
            throw new Error("HTTP ERROR");
    }
    ).catch(e => { showAlert("مشکل در شبکه") });
}

getData(tradeBaseUrl).then((obj) => {
        console.log(obj);
....

UPDATE更新

I made a stupid mistake.我犯了一个愚蠢的错误。 I have added an invisible character at the beginning of those two keys mistakenly.我错误地在这两个键的开头添加了一个隐形字符。 I found out the problem by inspecting the request body Network section, as Phil suggested.正如菲尔建议的那样,我通过检查请求正文网络部分发现了问题。 Two little pink circles were added at the beginning of key strings.在关键字符串的开头添加了两个粉红色的小圆圈。

Both keys contain a hidden character.两个键都包含一个隐藏字符。 If you open (edit) the snippet it shows, but you can also use charCodeAt to reveal it:如果您打开(编辑)它显示的代码段,但您也可以使用charCodeAt来显示它:

 const x = JSON.parse(`{ "bourseMarket":[2.9966187229626504E16,2.8923052836886444E16], "bourseTrade":[1.13589172355139E14,4.903299930043E13], "‌bourseMarketf":[6.037694517655877E15,5.876172638826381E15], "‌bourseTradef":[4.3646660180697E13,2.2708648590401E13] }`); console.log(`you would expect "bourseTradef".charCodeAt(0) to be be ${"bourseTradef".charCodeAt(0)}`); console.log(`but here it is ${"‌bourseTradef".charCodeAt(0)}`); console.log(`So x["‌bourseTradef"] is possible: ${x["‌bourseTradef"]}`); console.log(`But not x["bourseTradef"]: ${x["bourseTradef"]}`);
 .as-console-wrapper { top: 0; max-height: 100% !important; }

There is a zero width non-joiner (\‌) character at the start of "‌bourseMarketf" and "‌bourseTradef" in the sample.样本中“‌bourseMarketf”和“‌bourseTradef”的开头有一个零宽度非连接符(\‌)字符。 Javascript adds the quotation marks to indicate its presence. Javascript 添加引号以表明它的存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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