![](/img/trans.png)
[英]How do I use `JSON.parse`'s reviver function to add data to each object in an array?
[英]how to use reviver function with fetch.response.json()
JSON.parse 附帶一個“reviver” function (例如: “JSON.parse using reviver function” )。
如何將這個“復興者”與response.json一起使用? 例如:
fetch(url)
.then(a => a.json({reviver})) // unfortunately not working
.then(...)
我有一個解決方法:
fetch(url)
.then(a => a.text())
.then(b => new Promise((resolve) => resolve(JSON.parse(b, reviver))))
.then()
但它使用了更多步驟,這似乎沒用。 有更好的主意嗎?
您的解決方法基本上是最好的選擇,但如前所述,額外的 promise 是不必要的。
fetch(url)
.then(response => response.text())
.then(text => JSON.parse(text, reviver))
// ...
你可以這樣做:
fetch(url).then((response) => {return response.json()}).then((json) => {console.log(json)});
希望能幫助到你;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.