Javascript - convert an EXTRA LARGE Number to string in JSON before the default parsing

前端 未结 4 1083
走了就别回头了
走了就别回头了 2021-01-20 00:31

As I mentioned in THIS QUESTION, I have problem when getting the response from the server.

I receive an array of objects with these attributes:

[{
\"         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-20 00:58

    Transform the response to string, then apply a repalce with a regex to convert Id field to string type:

    const axios = require('axios');
    
    axios.get(url, { transformResponse: [data => data] }).then(response => {
      let parsed = JSON.parse(response.data.replace(/"Id":(\d+),/g, '"Id":"$1",'));
      console.log(parsed);
    });
    

提交回复
热议问题