如何从url
获取json
数据并保存到TypeScript
中的const变量中?
const data = (get json from url);
输出:
data = [
{
"name": "Peter",
"age": "20"
},
{
"name": "John",
"age": "25"
}
];
最佳答案
您可以使用 fetch API,这里是示例
我使用了动态变量,因为一旦生成响应就无法重新分配常量。
let data = ''
fetch('https://jsonplaceholder.typicode.com/comments')
.then(function(response) {
return response.json();
})
.then(function(myJson) {
data=myJson
console.log(data)
});
希望这对你有帮助:)
https://stackoverflow.com/questions/50642662/
相关文章:
python - 如何使用具有 NaN 值的 Pandas 计算中位数?
qt - 没有安装 qmlscene : why is a warning sign next to
php - 如何显示在 php 中使用 file_get_contents 检索到的 pdf?
xamarin - 类型 'StackLayout' 的值不能添加到类型 'IList' 的集合或字
angular - 如何指定从根目录到 Assets Angular 的路径?
reactjs - 我如何模拟 reactjs 中的 promise ?