我正在做一个货币转换项目,试图让我的代码在我的网站上显示JSON响应的结果,但我还不能做到这一点。
.then((response) => {
return response.json();
})
.then((jsonResponse) => {
let objData = {
amount: '',
from: '',
to: '',
result: '',
};
window.console.log('jsonresponse ==>' + JSON.stringify(jsonResponse));
let exchangeData = jsonResponse['query'];
window.console.log('exchangeData==> ' + JSON.stringify(exchangeData))
objData.amount = exchangeData['amount'];
objData.from = exchangeData['from'];
objData.to = exchangeData['to'];
objData.result = exchangeData['result'];
this.conversionData = objData;
window.console.log('objData=' + JSON.stringify(objData));
}).catch(error => {
window.console.log('callout error ' + JSON.stringify(error));
})
}
}
只返回“amount”、“from”和“to”而不返回“result”。如何让它返回转换的结果?这是示例JSON
{ "info": { "quote": 0.975625, "timestamp": 1669042263 },
"query": { "amount": 5, "from": "USD", "to": "EUR" },
"result": 4.878125,
"success": true
}
目前,我的代码只返回
"query": {
"amount": 5,
"from": "USD",
"to": "EUR"
我怎样才能让它返回
"query": {
"amount": 5,
"from": "USD",
"to": "EUR"
"result": 4.878125
我试过下面的,它没有工作。
const obj1={"query":[ { "amount":"", "from": "", "to": "" }, ] };
const obj2={"result":{}, };
const response = JSON.parse(JSON.stringify(obj1));
response.query.push(...obj2.result); console.log(response);
1条答案
按热度按时间8hhllhi21#
你问错地方了
其中
但
jsonResponse['query']
不包含结果。你应该做
得双曲余切值.