admin管理员组文章数量:1430083
I'm not familiar with JavaScript and Axios, so any help will be great :)
My request from Axios to URL has this response.
data: '{"id":"test1", "cool":"test2"}'
When trying JSON.parse like this
const buildArray = (await axios(config)).data
let toJSON = JSON.parse(buildArray)
It gives me an error
Unexpected token in JSON at position 0
Here it is my code and what I'm trying to do. For each Axios data response I want to add it to an array item and return it.
async function buildList(items, token){
try {
const list = []
let config = ''
for (item of items.items) {
config = {
method: 'get',
url: '/' + item.id,
headers: {
'Authorization': 'Bearer ' + token,
'Accept': 'application/json'
}
}
const buildArray = (await axios(config)).data
list.push(JSON.parse(buildArray))
}
return list
} catch (err) {
throw Error ('buildList error: ', err.message)
}
}
I'm not familiar with JavaScript and Axios, so any help will be great :)
My request from Axios to URL has this response.
data: '{"id":"test1", "cool":"test2"}'
When trying JSON.parse like this
const buildArray = (await axios(config)).data
let toJSON = JSON.parse(buildArray)
It gives me an error
Unexpected token in JSON at position 0
Here it is my code and what I'm trying to do. For each Axios data response I want to add it to an array item and return it.
async function buildList(items, token){
try {
const list = []
let config = ''
for (item of items.items) {
config = {
method: 'get',
url: 'https://my.domain./v1/customers/' + item.id,
headers: {
'Authorization': 'Bearer ' + token,
'Accept': 'application/json'
}
}
const buildArray = (await axios(config)).data
list.push(JSON.parse(buildArray))
}
return list
} catch (err) {
throw Error ('buildList error: ', err.message)
}
}
Share
Improve this question
edited Jul 10, 2020 at 2:14
Taguada
asked Jul 10, 2020 at 1:23
TaguadaTaguada
4861 gold badge5 silver badges8 bronze badges
2
-
I am not sure about the line
const buildArray = (await axios(config)).data
but as per the result you have shared you can parse it and it works here codepen.io/Maniraj_Murugan/pen/LYGrPXW .. – Maniraj Murugan Commented Jul 10, 2020 at 1:31 - Please, have a look at my question, I have updated it. I did the same test like yours, I know that it works, but I don't understand why when I get data from Axios response it does not work. – Taguada Commented Jul 10, 2020 at 2:01
4 Answers
Reset to default 0Is this OK?
const response = await axios(config) // waiting to response
const { data } = response // make sure we can get data
let toJSON = JSON.parse(data)
I have a couple of things you can try.
I'd remend you clarify your naming conventions. If the buildArray variable is an array then it can't be parsed unless in a JSON format. I'm continuing assuming it's the response data object you showed.
Try escaping the quotes inside the initial quotes. It would look like this:
data: "{\"id\":\"test1\", \"cool\":\"test2\"}"
Axios returns a promise. Try using it in Promise.then format rather than awaiting.
axios(config).then((response) =>
{
JSON.parse(response);
}).catch((err) =>
{
console.log("Failed: " + err);
});
Let me know if none of these work. Good luck :)
please make sure you are getting response data correctly. and if you return data as object at the backend side, you don't need to parse with JSON. JSON.parse is used for parsing string to JSON Pleas run this and check:
const buildArray = (await axios(config)).data;
console.log(buildArray );
I finally have found a workaround, but I don't know why Axios response was giving me a correct output of data response in everywhere I was looking at, but for some reason there was a weird ? (question mark) at the beginning of string, I only realised that after coping output and pasted it into a JSON validation website.
Basically from Visual Studio output it was showing correctly, something like that.
data: '{"id":"test1", "cool":"test2"}'
But for some reason when I copied this output from Visual Studio and pasted it into JSON validation website it gave me this result. A weird ? (question mark)
data: '?{"id":"49f362ad
So I tried to find a JSON regex validation, after running many different regex, on of them gave an undefined result instead of ? (question mark), so I tried this one below and worked, but this is bizarro and I'm not a developer man to discuss it further, I'm just sending this answer in order to share this result :)
list.push(JSON.parse(buildArray.replace(/[^\\"]+/).replace('undefined', '{')))
本文标签: javascriptAxios response data to JSONStack Overflow
版权声明:本文标题:javascript - Axios response data to JSON - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745556840a2663238.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论