Reputation: 485
I want to parse this api response to get the image url and I find it a little confusing actually because I'm new with apis.
{
"id": "123",
"item": [
{
"picture": {
"type_id": "2",
"url": [
"./img.jpg"
],
"is_in_description": 0,
"gallery": {
"url": "",
"url_id": ""
},
"layout_id": "2",
"variation_name": ""
},
"lister_id": "12345"
}
]
}
Here is my code for fetching the api, can anyone help me with that
fetch(url2,{
method: 'GET'
})
.then((response)=> response.json())
.then((responseJson) => {
const newImg = responseJson.item.map( => {
return{
const img =
};
})
const newState = Object.assign({}, this.state, {
items: newItems
});
console.log(newState);
this.setState(newState);
})
.catch((error) => {
console.log(error)
});
Upvotes: 0
Views: 905
Reputation: 1097
Use the map
method for parsing as
var x = {
"id": "123",
"item": [
{
"picture": {
"type_id": "2",
"url": [
"./img.jpg"
],
"is_in_description": 0,
"gallery": {
"url": "",
"url_id": ""
},
"layout_id": "2",
"variation_name": ""
},
"lister_id": "12345"
}
]
}
x.item.map(data=>{console.log(data.picture.url)}) //hope you need the url object
Upvotes: 1