【问题标题】:[Unhandled promise rejection: SyntaxError: JSON Parse error: Unrecognized token ''][Unhandled promise rejection: SyntaxError: JSON Parse error: Unrecognized token '']
【发布时间】:2021-07-09 11:43:37
【问题描述】:

我只想在反应本机错误中映射 JSON 数据,但在使用 JSON.parse 解析后,我收到错误“[Unhandled promise reject: SyntaxError: JSON Parse error: Unrecognized token '']”。

    axios.get("http://www.upclass.com.hk/api.php?apicall=getcustomer").then((e) => {
          var res = JSON.parse(e.data)
          console.log("New Data =>", res);
          this.setState({
                data: res
                });
    });

这是 JSON 数据

{
"id": "D6C9C876-CDA2-4C27-85BF-276D3035D762",
"fullname": null,
"username": null,
"email": "mohssssin@gmail.com",
"areaid": null,
"aid": null,
"mobile": "123456",
"photoid": null,
"imagepath": null,
"lang": "zh",
"custgroup": "P         ",
"IsVolunteer": null,
"notification": "Y",
"createdate": "2021-03-26 19:05:02.290",
"updatedate": null,
"otherarea": null
}

【问题讨论】:

  • e.data 已经是一个对象。你不需要用JSON.parse()解析它
  • @derpirscher,但我无法使用 this.state.data.id 映射它
  • @SuhailKakar - 然后在您的履行处理程序的第一行放置一个断点,并在调试器中查看e.data,看看您得到了什么。
  • @T.J.Crowder,在邮递员中很好,即使我浏览器我得到正确的数据,但是当我添加到我的代码时,我得到一个“?”在响应的开头

标签: json reactjs react-native expo


【解决方案1】:

尝试 JsonConvert.DeserializeObject 而不是 Parse。 You can check here

【讨论】:

  • 不太可能使用像 NewtonSoft.Json 这样的 .NET 库在 javascript 中工作......
猜你喜欢
  • 2021-11-09
  • 1970-01-01
  • 2020-07-03
  • 1970-01-01
  • 2021-06-07
  • 1970-01-01
  • 2018-06-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多