json - reactJS中JSON输入的意外结束
问题描述
我有来自 API 的以下 json
{"error":false,"session_id":"vb8pf4n7255deq4oj3tn2k1nlr","data":{"items":[{"type":"Obituary","object":{"obituaryId":"40811","deathDay":"2019-09-22","funeral":{"funeralDate":"2019-09-23","funeralTime":"16:00 ","place":"\u0643\u0646\u064a\u0633\u0629 \u0627\u0644\u0633\u064a\u062f\u0629\u060c \u0628\u0633\u0648\u0633\u060c \u0639\u0627\u0644\u064a\u0647","lat":"33.841211","long":"35.530477","communityId":"604"},"name":"\u0627\u0644\u0635\u064a\u062f\u0644\u064a \u0639\u0627\u0626\u062f\u0629 \u064a\u0648\u0633\u0641 \u063a\u0646\u0637\u0648\u0633 \u0635\u0627\u062f\u0631","photo":"http:\/\/khoolood.kh\/\/css\/images\/defaults\/obituary.png","categoryId":"8"}},{"type":"Obituary","object":{"obituaryId":"40810","deathDay":"2019-09-22","funeral":{"funeralDate":"2019-09-23","funeralTime":"16:00 ","place":"\u00c9glise Notre-Dame, \u00e0 Bsous, Aley","lat":"33.841211","long":"35.530477","communityId":"604"},"name":"A\u00efda Youssef Ghantous Sader","photo":"http:\/\/khoolood.kh\/\/css\/images\/defaults\/obituary.png","categoryId":"8"}},{"type":"Obituary","object":{"obituaryId":"40809","deathDay":"2019-09-20","funeral":{"funeralDate":"2019-09-22","funeralTime":"15:00 ","place":"\u0643\u0627\u062a\u062f\u0631\u0627\u0626\u064a\u0629 \u0633\u064a\u062f\u0629 \u0627\u0644\u0628\u0634\u0627\u0631\u0629 \u0644\u0644\u0633\u0631\u064a\u0627\u0646 \u0627\u0644\u0643\u0627\u062b\u0648\u0644\u064a\u0643\u060c \u0627\u0644\u0645\u062a\u062d\u0641","lat":"33.877744","long":"35.516491","communityId":"647"},"name":"\u0623\u0646\u0637\u0648\u0627\u0646 \u062a\u0648\u0641\u064a\u0642 \u0639\u0628\u062f \u0627\u0644\u0648\u0627\u062d\u062f","photo":"http:\/\/khoolood.kh\/\/css\/images\/defaults\/obituary.png","categoryId":"8"}},{"type":"Obituary","object":{"obituaryId":"40807","deathDay":"2019-09-20","funeral":{"funeralDate":"2019-09-21","funeralTime":"16:00 ","place":"\u0643\u0646\u064a\u0633\u0629 \u0645\u0627\u0631 \u0625\u0644\u064a\u0627\u0633\u060c \u0627\u0644\u0644\u064a\u0644\u0643\u064a\u060c \u0627\u0644\u0645\u0631\u064a\u062c\u0629\u060c \u0628\u0639\u0628\u062f\u0627","lat":"33.833957","long":"35.513066","communityId":"1307"},"name":"\u062c\u0627\u0646 \u0637\u0627\u0646\u064a\u0648\u0633 \u0639\u0637\u0627\u0627\u0644\u0644\u0647","photo":"http:\/\/khoolood.kh\/\/css\/images\/defaults\/obituary.png","categoryId":"8"}},{"type":"Commemoration","object":{"obituaryId":"40796","deathDay":"2018-09-24","funeral":{"funeralDate":"2019-09-24","funeralTime":"12:00","place":"\u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631","lat":"","long":"","communityId":""},"name":"\u0627\u0644\u062f\u0643\u062a\u0648\u0631 \u0623\u0646\u0637\u0648\u0627\u0646 \u062c\u0648\u0632\u0641 \u062e\u0628\u0651\u0627\u0632","photo":"http:\/\/khoolood.kh\/\/css\/images\/defaults\/obituary.png","categoryId":"6"}},{"type":"Commemoration","object":{"obituaryId":"40795","deathDay":"2018-09-24","funeral":{"funeralDate":"2019-09-24","funeralTime":"12:00","place":"\u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631","lat":"","long":"","communityId":""},"name":"\u062c\u0627\u0646 \u0643\u0644\u0648\u062f \u0645\u0631\u0633\u0627\u0644 \u062d\u0631\u0641\u0648\u0634","photo":"http:\/\/khoolood.kh\/\/css\/images\/defaults\/obituary.png","categoryId":"6"}},{"type":"Commemoration","object":{"obituaryId":"40794","deathDay":"2018-09-24","funeral":{"funeralDate":"2019-09-24","funeralTime":"12:00","place":"\u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631","lat":"","long":"","communityId":""},"name":"\u062f\u0648\u0631\u0627 \u0644\u0648\u064a\u0633 \u0635\u0627\u0644\u062d","photo":"http:\/\/khoolood.kh\/\/css\/images\/defaults\/obituary.png","categoryId":"6"}},{"type":"Commemoration","object":{"obituaryId":"40793","deathDay":"2018-09-24","funeral":{"funeralDate":"2019-09-24","funeralTime":"12:00","place":"\u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631","lat":"","long":"","communityId":""},"name":"\u0631\u064a\u0645\u0648\u0646 \u0625\u0644\u064a\u0627\u0633 \u0645\u0635\u0631\u064a","photo":"http:\/\/khoolood.kh\/\/css\/images\/defaults\/obituary.png","categoryId":"6"}},{"type":"Commemoration","object":{"obituaryId":"40792","deathDay":"2018-09-24","funeral":{"funeralDate":"2019-09-24","funeralTime":"12:00","place":"\u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631","lat":"","long":"","communityId":""},"name":"\u0627\u0644\u0623\u0633\u062a\u0627\u0630 \u0623\u0646\u064a\u0633 \u0645\u062d\u0645\u0648\u062f \u0623\u0628\u0648 \u0631\u0627\u0641\u0639","photo":"http:\/\/khoolood.kh\/\/css\/images\/defaults\/obituary.png","categoryId":"6"}},{"type":"Commemoration","object":{"obituaryId":"40791","deathDay":"2018-09-24","funeral":{"funeralDate":"2019-09-24","funeralTime":"12:00","place":"\u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631","lat":"","long":"","communityId":""},"name":"\u064a\u0648\u0633\u0641 \u0625\u0644\u064a\u0627\u0633 \u0627\u0644\u062d\u0648\u064a\u0643","photo":"http:\/\/khoolood.kh\/\/css\/images\/defaults\/obituary.png","categoryId":"6"}},{"type":"Commemoration","object":{"obituaryId":"40790","deathDay":"2018-09-24","funeral":{"funeralDate":"2019-09-24","funeralTime":"12:00","place":"\u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631","lat":"","long":"","communityId":""},"name":"\u0627\u0644\u0623\u0633\u062a\u0627\u0630 \u062c\u0648\u0631\u062c \u064a\u0648\u0633\u0641 \u0636\u0648\u0651","photo":"http:\/\/khoolood.kh\/\/css\/images\/defaults\/obituary.png","categoryId":"6"}},{"type":"Obituary","object":{"obituaryId":"40789","deathDay":"2019-09-14","funeral":{"funeralDate":"2019-09-17","funeralTime":"17:00","place":"\u0643\u0627\u062a\u062f\u0631\u0627\u0626\u064a\u0629 \u0627\u0644\u0642\u062f\u064a\u0633 \u0646\u064a\u0642\u0648\u0644\u0627\u0648\u0633 \u0644\u0644\u0631\u0648\u0645 \u0627\u0644\u0623\u0631\u062b\u0648\u0630\u0643\u0633 (\u0645\u0627\u0631 \u0646\u0642\u0648\u0644\u0627)\u060c \u0632\u062d\u0644\u0629","lat":"33.847231","long":"35.903866","communityId":"706"},"name":"\u0627\u0644\u0645\u062a\u0631\u0648\u0628\u0648\u0644\u064a\u062a \u0625\u0633\u0628\u064a\u0631\u064a\u062f\u0648\u0646 \u062e\u0648\u0631\u064a","photo":"http:\/\/khoolood.kh\/storage\/obituary\/13662608021333066283.jpg","categoryId":"8"}},{"type":"Commemoration","object":{"obituaryId":"40788","deathDay":"2002-09-23","funeral":{"funeralDate":"2019-09-23","funeralTime":"12:00","place":"Non Fourni","lat":"","long":"","communityId":""},"name":"Abdel Majid Youssef Bey El-Zein","photo":"http:\/\/khoolood.kh\/\/css\/images\/defaults\/obituary.png","categoryId":"6"}},{"type":"Commemoration","object":{"obituaryId":"40787","deathDay":"2002-09-23","funeral":{"funeralDate":"2019-09-23","funeralTime":"12:00","place":"\u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631","lat":"","long":"","communityId":""},"name":"\u0639\u0628\u062f \u0627\u0644\u0645\u062c\u064a\u062f \u064a\u0648\u0633\u0641 \u0628\u0643 \u0627\u0644\u0632\u064a\u0646","photo":"http:\/\/khoolood.kh\/\/css\/images\/defaults\/obituary.png","categoryId":"6"}},{"type":"Commemoration","object":{"obituaryId":"40775","deathDay":"2018-09-23","funeral":{"funeralDate":"2019-09-23","funeralTime":"12:00","place":"\u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631","lat":"","long":"","communityId":""},"name":"\u0623\u0646\u062c\u0644\u064a\u0646\u0627 \u064a\u0648\u0633\u0641 \u0645\u0627\u0631\u0648\u0646","photo":"http:\/\/khoolood.kh\/\/css\/images\/defaults\/obituary.png","categoryId":"6"}}]}}
当我在 jsonlint.com 上运行它时它是有效的,但是当我将它传递给 react 时,我得到一个错误“JSON 输入意外结束”。这是反应的代码:
componentDidMount() {
this.setState({loading: true})
fetch('http://domainname.kh/en-lb/v2/api?action=recentObituaries')
.then(data => data.json()) //error on this line
.then(data => this.setState({data, loading: false}))
}
如果我编辑 API,则错误已修复,返回如下:
echo "[" . json_encode($response) . "]";
而不是这个:
echo json_encode($response);
感谢您的帮助,我刚开始学习反应。
解决方案
我放了工作示例,但无法从代码中的地址获取:还尝试将 data.json() 替换为 data.text() 并制作 JSON.parse https://codesandbox.io/s/react-example- mpdhn
推荐阅读
- node.js - 如何在Nodejs的for循环中使用回调函数
- postgresql - psycopg2 错误:未加载库:运行 tox pytest 时的@rpath/libssl1.1.dylib
- php - 更改 htaccess 后出现内部服务器错误
- powershell - Powershell 文件通过 Stream 传输到远程计算机失败
- php - 如何更改select2上项目的文本颜色
- python - 向经常宕机的主机发送 UDP
- javascript - 在 ag-Grid 中关注多个单元格(如在 Excel 中单击和拖动鼠标)
- angularjs - 路由重定向中来自 $http 调用的用户值
- python - 如何通过 FTP_TLS 上传文件
- javascript - 在另一个函数中返回询问者提示的答案