I have encountered a weird problem. I am consuming a RESTful API which returns the following response body:
{
"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:
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. 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.
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.
resp.okinstead ofresp.status === 200as a more reliable way to detect a successful responseobj['"bourseTradef"']